Try Catch How does it work ?

Monkey Forums/Monkey Programming/Try Catch How does it work ?

golomp(Posted 2013) [#1]
Hi,

I tried to use Try Catch to handle errors but i don't know how it works.

For example i try to handle a divide error by 0.
I create a simple loop program to create this error and handle it
but it fail.

So i have two questions :
1) How can i use Try Catch efficiently to manage an error ?
2) When i manage the error, how can i reset the error flag ?
Import mojo

Function Main()
	New MyGame
End

Class MyGame Extends App
	Field x:Float=0
	Field r:Float=0

	Method OnCreate()
		SetUpdateRate 5
		r=0
	End
	
	Method OnUpdate()
		Local calculation:Float
		Try
			x=r-1
			calculation=x/x;
			Print "calculation possible "+x
			Throw New Throwable
			Print "never shown"
		Catch ex:Throwable
			Print "calculation impossible "+x
		End
	End

	Method OnRender()
		r=r+1
		If r=3 Then Error("")
	End
		
End


Thank you


Paul - Taiphoz(Posted 2013) [#2]
Wait what are you expecting to happen or what actually happens for you when you run this, try/catch isnt something I'v used but for me it seems to be doing what I would expect it to.

does not crash and throws the correct error when it trys a div/0 ?


golomp(Posted 2013) [#3]
Hi Taiphoz,

I would like to handle an error.

For example in the sample code, i make a simple calculation.

During the loop, when x=0, the calculation generate an error : 0/0
I would like to handle this error and show a message "calculation impossible"

After what i would like to reset the error flag to make the program
able to manage an other error.

Thanks


Paul - Taiphoz(Posted 2013) [#4]
nvm ....... just noticed its print both msg's


Paul - Taiphoz(Posted 2013) [#5]
I also recall mark saying at one point that dividing by zero is a runtime error and try/catch cant catch runtime errors, but im sure that was a few versions ago and a lot can change from version to version..

its a shame but this is another one of those things that seriously lacking in the docs


golomp(Posted 2013) [#6]
In fact Taiphoz, the error i try to manage concern a big project on Android
(more than 500 lines) and a TCP/IP connexion problem.
When there are no wifi connexion, it crash with a monkey error message
and after it crash the smartphone. (i am obliged to remove battery)

So the kind of error is not important to me.

I just want to know how to handle an error with monkey and how to reset the error flag to be able to handle an other one if it happens.

If there is a solution with Try/Catch it's ok for me.
If there is another way to do, it's also ok for me...


Paul - Taiphoz(Posted 2013) [#7]
http://monkeycoder.co.nz/Community/post.php?topic=3098&post=31782


golomp(Posted 2013) [#8]
Thank you Taiphoz, I already read this link but it doesnt help me.

The error message i have in my game App is :
"Monkey Runtime Error : Attempt to access null object"

It appear if i do a FTP transaction without Wifi connexion.

That's why i have two options :
1) detect if there is a Wifi connexion available => i already create a topic on Android Monkey section but i have no solution
2) handle the error => this topic.

Or 3) in a futur Monkey version Mark talk about include a kind of OnError() function...


AdamRedwoods(Posted 2013) [#9]
Import mojo

Function Main()
	New Game
End


Class Game Extends App
	Method OnCreate()
	 SetUpdateRate 30
	End
	
	Method OnUpdate()
		
		Try
			Print MyMath.Divide(5,6)
			Print MyMath.Divide(5,0)
		Catch ex:MyMath
			Print ex.err
		End
		
		Error ""
	End
	
	Method OnRender()
	
	End
	
End

Class MyMath Extends Throwable
	Field err:String = "Error: divide by zero"
	
	Function Divide:Float(a:Float,b:Float)
		If b=0 Then Throw New MyMath
		Return a/b
	End
End



golomp(Posted 2013) [#10]
Hi Adam,

Thank you I better understand how Try Catch works...

If i had this line :
			Print MyMath.Divide(5,2)
under the two previous Print, it doesnt show 2.5.

In the same idea, if i exchange the two Print order like this :
			Print MyMath.Divide(5,0)
			Print MyMath.Divide(5,6)
I just see the error message.

Is there a possibility to shutdown the error flag to pursuit normal execution
after handling an error ?


AdamRedwoods(Posted 2013) [#11]
Is there a possibility to shutdown the error flag to pursuit normal execution
after handling an error ?

not that i know of. you would almost have to do a try/catch for EACH call to divide.


golomp(Posted 2013) [#12]
Very goog idea Adam, i am going to "try" this solution. :)
Thank you.


golomp(Posted 2013) [#13]
I just made my test with a little loop from -1 to 1
and it works exactly as you expect :
-5
Error: divide by zero
5
Now i know exactly how it works. Thank you Adam.

My last question is : in the App i want to handle error, i have no possibility
to make a test like
If b=0 Then Throw New MyMath

Is there a way to detect an error happened in Monkey ?


golomp(Posted 2013) [#14]
I made a search in forums with "runtime" keyword but i didn't find
something to do this. If any one has an idea...


Paul - Taiphoz(Posted 2013) [#15]
Thanks from me as well Adam that example helped me understand what was going on better as well.. still not sure I would use it tho. but at least if the need does arise I have a better understanding of what would be required.

now that I think of it, I am sure Diddy actually has a whole exception system setup, I might look at that.


AdamRedwoods(Posted 2013) [#16]
Is there a way to detect an error happened in Monkey ?

Yeah, you'd think that, but it depends.

Things like this need to be enabled and handled by the compiler:
Be careful: the catch clause does not handle C exceptions and system- or application-generated exceptions such as memory protection, divide by zero when compiled with default compiler switch /EHsc (C/C++->Code Generation->Enable C++ Exceptions: Yes). See "Exception Handling: Default Synchronous Exception Model" http://msdn.microsoft.com/en-us/library/7f10tsf4(VS.80).aspx and "/EH (Exception Handling Model)" http://msdn.microsoft.com/en-us/library/1deeycx5(VS.80).aspx for details.



golomp(Posted 2013) [#17]
Hard problem.

I think i am going to wait for the OnError() method or function Mark think in a future version.

Thank you Taiphoz and Adam for your answers.