Catch?
BlitzMax Forums/BlitzMax Beginners Area/Catch?
| ||
Local aValue$[] = ["Simon" , "Dave"] Print "array length=" + Len(avalue) Try Print avalue[0] Print avalue[1] Print avalue[2] Catch a$ Print a$ EndTry Above is some sample code, why doesn't catch catch the error? The example in the help seems to imply that catch will only catch an exception you throw yourself? |
| ||
Local aValue$[] = ["Simon" , "Dave"] Print "array length=" + Len(avalue) Try Print avalue[0] Print avalue[1] Print avalue[2] Catch exception:Object Print exception.ToString() EndTry |
| ||
Thanks yan That's more like what I was expecting, the more I look at the examples in the help files the less help i seem to get! How do you get the code window? |
| ||
Yeah, the try/catch command examples are a bit misleading. :o/ Have a look at 'Language>Exceptions', it's much more helpful. What are the forum codes? |
| ||
I use the code you use, with Catch A$ and print A$, seems to trap opengl errors okay. Also did you get a catch when compiling with debug on? |
| ||
Catch A$ won't trap anything other than strings being thrown, so it will fail on most exceptions. |
| ||
Try this code from my framework (you may need to modify it):Function HandleGeneralError(o:Object) 'This should be called inside the Catch section of a Try/Catch/End Try like this: 'Try ' 'some code 'Catch o:Object ' HandleGeneralError(o) ' Shutdown() 'End Try EndGraphics 'safety to ensure desktop isn't messed up ShowMouse() 'make sure user can click on dialog. 'In debug mode, this will display a valid error message. 'In non-debug mode (final release), this will show a dialog saying "memory execption error" 'which is better than nothing. 'In full-screen mode, this will still show a dialog on the desktop after the graphics mode 'is ended, and when the dialog is clicked, the game will shutdown. If TBlitzException(o) Then Local Prefix$ = "" If DebugInt<> 0 Then Prefix = "("+DebugInt+") " Notify Prefix + TBlitzException(o).ToString() Else 'Perhaps a string has been raised? If so, show it. Notify o.Tostring() EndIf End Function |
| ||
You can have multiple catch blocks:Try ... Catch a$ ... Catch Ex:TBlitzException ... Catch b:Object ... EndTry |
| ||
Man, I've read the Blitz documentation, I've read these posts, I've programmed for a decade now ... but I still have no idea what Try and Catch do. |
| ||
I still have no idea what Try and Catch do. If an error occurs inside the try..catch block, BMax throws an exception (which catch captures), you can then handle the error between the Catch and End Try block. |
| ||
Hmm that's good to know, I didn't know I was only catching strings. Thanks. |
| ||
Ok; I understand the Try-Catch a bit better now, but... Even with the code posted by "yan" or "Grey alien", the catch statement still does not catch the error in the code I posted. Local aValue$[] = ["Simon" , "Dave"] Print "array length=" + Len(avalue) Try Print avalue[0] Print avalue[1] Print avalue[2] Catch o:Object Print o.ToString() EndTry This still fails with a nasty application error "Attempt to index array element beyond array length". |
| ||
It works if debug is on, maybe release mode doesn't generate the checking code? |
| ||
Try-Catch is a debug only thing (would be nice if it weren't), the docs should really be updated to reflect this. |
| ||
That would make it totally useless as Try Catch is meant to give usefull error message if it fails in release mode?! The problem with above examples: The Array out of bound check etc are only performed during Debug so they actually can not fire anything in release, they just break. |
| ||
To Perturbatio: It appears that Try-Catch does work with both debug and release versions of BlitzMax programs. However, I believe the reason that doswelk's code broke during release version was that array bounds checking is disabled -- therefore no exception was thrown. If it had been thrown, I believe it would have been caught as expected. |
| ||
Five Months, just to say exactly what the pevious post said ;) Mind you Try-catch does need some more examples in the docs. |
| ||
I think slowly ;-). When I read the thread I was left with the impression that Try-Catch was only debug mode, which is incorrect. I wanted to make it clear to subsequent readers that it works with release versions as well. But yes, after I posted, I realized that the array explanation had been pointed out by Dreamora. And I agree: more examples are needed, as well as examples of creating custom exception objects. So, here's one:Type invalidValueException Extends TBlitzException field msg:string = null Method toString:String() if msg then return msg else return "Invalid value!" End Method End Type And it's thrown like this: function strToBool:byte(s:string) select s.toLower().trim() case "true" return True case "false" return False default local e:invalidValueException = new invalidValueException e.msg = "Bad value passed: " + s Throw e end select end function and caught like this: try b = strToBool(s) catch ex:invalidValueException print ex.toString() ' Do some error recovery things here catch ex:object ' Catch other exceptions print ex.toString() ' Do some other things here end try |