Try catch block

Monkey Forums/Monkey Beginners/Try catch block

Sensei(Posted 2015) [#1]
Hey guys,
Pardon my stupidity, but I was wondering about how to use the Try Catch block as explained in the docs.
I find that sometimes I have an error in code that doesn't clearly indicate where the problem lies and it could take ages to try trace where the error originates from, hence why I wondered if you could use Try Catch blocks in Monkey-X to help with that kind of troubleshooting.
Suffice to say, I followed the example and put a try catch block in the code where I knew the error was originating from but it didn't do anything.
Am I misunderstanding the use of this?


Nobuyuki(Posted 2015) [#2]
Try..Catch blocks are used to catch exceptions in code that you've thought of ahead of time to handle. Monkey's exception handling isn't particularly helpful, since uncaught exceptions are always handled with the same error message (The error message doesn't reflect the type of exception being thrown, and you aren't able to specify to trans any more specific error message within the exception class itself). However, if you are aware of the typical kinds of exceptions that can occur, you can account for them in your code and be able to throw those exceptions where they would normally occur.

Try..Catch does not prevent runtime errors, which are considered more serious than exceptions, and thus aren't going to help you if you do something like make a null reference. Many of Mojo's loading routines do not throw exceptions when loading fails, so encapsulating some code in a Try..Catch block will not stop an error that results from a null reference. Instead, what you should be doing to examine your code is to look at the call stack at the time of the error. Compiling in debug mode will helpfully add the necessary sauce needed to generate a call stack when the runtime hits an error.


And now, to explain a little bit about exceptions. Throwable is a base class used by Try..Catch blocks to perform tasks based on types of exceptions which are caught. You can extend this class to create specific exceptions (circumstances) which other coders may run into when using your code. For example, my Table<T> class contains empty classes for TableInitError and TableIndexOutOfRangeError, which are thrown when invalid ranges are specified in the constructor, or in a Getter/Setter, respectively. These classes are empty because the base class is empty. If I wanted to, though, for a standard framework I could make sure all of my Throwables had a Description property, which other coders could use when performing a Catch on say MyBaseThrowable, which contained a Description property. (Why Mark decided to make Throwable contain no members is beyond the scope of this post, lol.) The Throwable, then, could be used to gain more information about the specific exception, which could help the end-coder determine how to handle the exception.

I hope this answered your questions.


Sensei(Posted 2015) [#3]
Thank you Nobuyuki. That makes more sense to me now, but unfortunately doesn't help me much as you explained above. Best solution is to make my classes more modular in size and files so that when errors are thrown, it will be easier to see where it's coming from instead of a 10000 line long code file :)
Luckily I have been doing this more over time as I've learnt to use Monkey-X. Just a pity you can't use Try-Catch blocks like I hoped and as it stands, still confuses me as to the real benefits of using it. Can you give me a light real-world example of how to go about using it in a way to look at the call stack?


Nobuyuki(Posted 2015) [#4]
Try..Catch won't show you the call stack, it only serves as a way to handle thrown exceptions at runtime in a way that is typically invisible to the end-user. You can make caught exceptions throw an error (using Error()), but this won't usually help you, because I don't believe it will provide an accurate call stack of where the exception occurred but instead a call stack of the point where you stopped execution.

Call stacks are generated automatically in Debug mode whenever program execution stops due to an error. Check your output console.