Throw Catch Try - Cany anyoen give an example ?

BlitzMax Forums/BlitzMax Beginners Area/Throw Catch Try - Cany anyoen give an example ?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Exception handling.
Try
	Repeat
		a:+1
		DrawText (a+"is OK",400,300)
		If a>5 Throw "chunks"
		Flip
		Cls
		Delay 100
	Forever

Catch a$
	cls
	DrawText ("caught exception "+a$,400,300)
	flip
	waitmouse
EndTry


essentially it is in the format of:

try > throw > catch > end try

the try / end try block is also referred to as an Exception Block, you are trying to isolate a behavior

throw forwards a caught exception

catch handles the exception:

here is another example that is a little more useful:

Rem
Throw generates a BlitzMax exception.
End Rem

Graphics 800,600,0

Local bmax:String[]=["one","two","three","four"]
 

Try
	Repeat
		a:+1
		DrawText (bmax[a],400,300)
		If a > 4 Then Throw "BAM!" 
				Flip
		Cls
		Delay 300
	Forever

Catch ex:Object
	cls
	DrawText (ex.Tostring() ,10,300)
	flip
	waitmouse
EndTry