Try Catch?

BlitzMax Forums/BlitzMax Beginners Area/Try Catch?

Yue(Posted 2016) [#1]
What does a try catch ?, is something new that is not Blitz3D


grable(Posted 2016) [#2]
It catches exceptions trown within it, exceptions can be any Object.
Try
  doit()
Catch e:Object
  Print "oops: " + e.ToString()
EndTry
Print "done"
End

Function doit()
  Print "enter"
  Throw "ERROR"
  Print "leave"
EndFunction



Yue(Posted 2016) [#3]
@grable.
Method G3D()
	 
	    
	   
		
	Try 
	
	 
		    If( xGfxModeExists( Self.anchoPantalla:Int, Self.altoPantalla:Int, Self.pdadColor:Byte ))
	            
	           xGraphics3D ( Self.anchoPantalla:Int, Self.altoPantalla:Int, Self.pdadColor:Byte, Self.modoVentana:Byte, Self.vSync:Byte )            
	           xSetBuffer( xBackBuffer() )
	        Else 
	
	           Throw "Graphics3D"
	        End If 
	 Catch t$
	         Print "Error :" + t$
	         End 
	 EndTry 
	
	End Method

Ok?


grable(Posted 2016) [#4]
Well it works. But i see no reason to use it in that case, as you are in the same scope.
Its better to use Try/Catch around code that might throw exceptions deeper in the call stack, usually by things you have little control over.

Also notice that when thrown, it unwinds the stack and returns the program to any outer Catch statements of which there can be multiple levels.
To let another level handle the exception it then has to be re-thrown.

Btw, its generally bad to use exceptions for control-flow. Exceptions should be exceptional ;)


Yue(Posted 2016) [#5]
Ok, Thanks You. =)


Kryzon(Posted 2016) [#6]
There's some more information here:
http://www.blitzbasic.com/Community/posts.php?topic=105700