Finally try/catch with MTASC
Posted on Monday, November 06, 2006 @ 23:27 CET
Don't mind the pun :)
Earlier I was trying to make some code more robust by setting up try/catch blocks within some helper classes. These exceptions would then bubble upwards, each level adding its own summary to the exception creating a pseudo-stack trace where you easily see exactly where the error occurred and under what circumstances. Trust me, it sure beats a whole slew of trace statements everywhere.
Now after playing with AS3 in Flex2 lately I've been trying to take some of these ideas back into my AS2 projects, normally with good results in order to get some of the loveliness found in AS3. For example, writing a simple Event class to structure an object for the EventDispatcher. By rolling out your own, basic Event class you avoid coding against a black-box Object, and so on. These simple structs allow you to bid adieu to the ObjectDumper and are great when working in a team.
However, I found some funky issues when trying to catch exceptions in FlashDevelop, which compiles using MTASC. I've been using MTASC's "strict-mode" lately because it forces you to type absolutely everything (method parameters, functions, etc) which can save you some headaches later on. When using some of Macromedia's classes you have to do a little editing here and there because these can be a little sloppy, but no biggy (mx.events.EventDispatcher for example has 2 objects not typed to Object).
In any case, I was trying to catch an exception with catch (e) but MTASC complained because e wasn't typed. Now I was throwing a simple string with throw("Crash!"), so I figured I could type it as a String, catch(e:String). But the exception was never caught.
However, tracing out the typeof of the untyped variable e said it was a "string". Weird. Even weirder was that catch(undefined) was also caught!
Note that this behaviour only occurs with MTASC. MMC (the Flash IDE compiler) catches the exception when typed to a String when thrown in the manner described above, and does not catch undefined (it considers it an error and won't compile). But I'm using MTASC to avoid coffee-breaks while things compile, so I played with it some more and found a solution.
When throwing an exception, cast it as something. So for the example above it became throw(new String("Crash!")). The String was caught as it should be. This is obvious to Java/C# heads I'm sure, but seeing as though throw() accepts anything, it wasn't immediately obvious.
Which brings me back to the AS3 way of doing things. Instead of throwing Strings or Objects you could roll out your own set of Exception classes to handle whatever it is you need in order to tighten your code even more and provide you with a better night's sleep.
Update: Doh! You could simply use the Error class which, I'm guessing, exists for precisely this reason. I've used it before in AS3 but for some reason didn't think it existed in AS2. How have you remained hidden in the shadows all this while?
- paulo




Post a comment:
You must have Flash and JavaScript enabled to post a comment.