Exception not being hanlded?

Monkey Forums/Monkey Programming/Exception not being hanlded?

ziggy(Posted 2012) [#1]
This doesn't handle the exception:
[monkeycode]Function Main()
try
Local a:List<String>
a.AddLast("Hello!")
Catch err:THROWABLE
Print "Handled!"
End Try
End[/monkeycode]
I'm posting this here as I'm not sure it's a bug or not, but looks very weird.


marksibly(Posted 2012) [#2]
Hi,

In Monkey, null object, array index and int divide by zero errors aren't exceptions, but runtime errors.

Runtime errors can't be caught - they are bugs in your program that need to be fixed. The Error function also generates such runtime errors.

I may add an OnError() handler similar to OnCreate() to allow you to execute some last minute cleanup code etc. in case you're writing code for nuclear reactors in Monkey, but in general the above errors should be considered 'irrecoverable'.

There are several reasons I didn't attempt to convert runtime errors to exceptions...

* Getting consistent behavior on all targets/all configs would have been difficult. ie: I would have had to enable runtime array index checks on the c++ release target, and added some divide by zero checking to JS/AS. Likely more. But IMO the above errors are programming errors and your app *shouldn't* continue anyway.

* The monkey modules are not necessarily 'exception safe', and writing exception safe code is not trivial...

http://en.wikipedia.org/wiki/Exception_safety#Exception_safety

So, if the list or mojo or whatever module were to throw an exception, with the way the code is now, there's just no way your code could assume everything was hunky dory and continue anyway.


muddy_shoes(Posted 2012) [#3]
I may add an OnError() handler similar to OnCreate() to allow you to execute some last minute cleanup code etc. in case you're writing code for nuclear reactors in Monkey


Non-safety-critical systems need to catch errors and log failure information too.


ziggy(Posted 2012) [#4]
It's ok, just wanted to be sure it was not a bug on the new exceptions implementation.

EDIT: I'm a bit with muddy-shoes here. Who says a Super Star Soccer match is not critical? hehe. Now, being serious, one of the things I really love about C# or Java is that you can get a lot of information when an exception occurs (call stack as instance) and it can help you a lot narrowing things on complex realtime applications (you know the typical bug that only happens when the user makes a chain of things in an specific order etc.) In this areas, being able to catch exceptions and inspect whatever has gone wrong by inspecting the call stack information that was involved in the exception being thrown can be very useful. I supose it can be way too complex, so maybe it should not be a priority, I don't know... This is just my impression on Monkey exceptions. Maybe it should be left this way, and expect this level of detail to be done into the native languages. I have never touched anything like expections on C++ or ActionScript, so I understand it may not be possible.


ziggy(Posted 2012) [#5]
Have you seen the mention of BlitzMax on the article about exception safety? It's cool :)


sereschkin(Posted 2015) [#6]
Any news on this? In my code I have to execute foreign Monkey code and this is not always null-safe. Now I have no chance to catch an error, but modify given code to get around null access, right?

EDIT: I guess not :(