AS3.0 TutBits #06: Exception handling
Actionscript uses a rather loose throw convention, since you can throw nearly everything - it does not have to be an exception. The throw statement thus looks like this:
throw expression
According to that, the following code is perfectly valid and outputs “Test” :
public function Main() { try { throwSomething(); } catch ( e:String ) { trace(e); } } private function throwSomething():void { throw new String("Test"); }
However, this is not the normal case. In actionscript you should always throw Error Objects. So, ideally above’s code should be look like this:
public function Main() { try { throwSomething(); } catch ( e:Error ) { trace(e.message); } } private function throwSomething():void { throw new Error("Test"); }
Of course, you could write your own exception classes which derives from the Error class like you would do it in other languages with the Exception class. Apart from that difference, Actionscript’s Exception handling is pretty straight-forward and sticks to the general paradigm.
