Wednesday, March 5, 2008

Exception to the exception rule

Generally I like to catch exception at the boundary between systems. When working on a tightly integrated javascript/flash application my first instinct is to catch the exception around the functions that are called from javascript inside flash. Some sample code would be the following:



public function init() {
if (ExternalInterface.available) {
trace( "ExternalInterface available" );
ExternalInterface.addCallback( "fromJs", fromJs );
}
}

public function fromJs( somearg:String) : String {
try {
// implementation here
} catch( e:Exception ) {
trace( "fromJs blinked " + e );
return "Error " + e;
}
return "Ok";
}

}


Incidentally I recommend always returning something from your callback functions and outputting it to the firebug console, it helps you zoom much faster on the problem.

The problem with catching the exception is that you lose the stack trace. In this technology you are much better off letting the built in exception handler output the exception with the full stack trace. Just make sure you are watching the flashout.txt file. If you do not know where that file is search for 'flex mm.cfg'. Flex 3 will install the debug mode Flash Player for you, if you do not use the debug mode Flash Player you are charging your customers 10 times more money than you should. Bad boy :)

No comments: