Handling Exceptions

Exceptions are errors that propagate upwards until they are correctly handled by a previously registered handler. To throw an exception, you can use the throw statement :

   throw "Error while doing something";

In Haxe, you can throw any value : strings, integers, objects, enums....

You can catch an exception using a try...catch block :

try {
    // ...
    throw "Error";
} catch( msg : String ) {
    trace("Error occurred: " + msg);
}

When not using curly braces you omit a semicolon from the try statement before the catch :

try throw "Error"
catch( msg : String ) trace("Error occurred: " + msg);

You can have several catch blocks with different types to handle different kinds of exceptions. The type Dynamic will catch all exceptions :

try {
   // ...
} catch( msg : String ) {
   trace("Error message : " + msg );
} catch( errorCode : Int ) {
   trace("Error #"+errorCode);
} catch( unknown : Dynamic ) {
   trace("Unknown exception : "+Std.string(unknown));
}

Uncaught exceptions

When an exception is not caught by any catch, it can bubble up to the top level of the application : it's an uncaught exception. In that case, each platform have a specific behavior.

In JavaScript

In JavaScript, uncaught exceptions are displayed in the JS Console. You can see it by displaying it on Firefox in Tools/JavaScript Console or in Internet Explorer by clicking the error icon displayed at the lower left of the browser window.

If you want to handle uncaught exceptions by yourself, you can use an error handler. In this example, the function will use the alert function to display the uncaught exception in a message box :

class Test {

    static function myHandler( msg : String, stack : Array<String> ) {
        js.Lib.alert(msg);
        return true;
    }

    static function main() {
        js.Lib.setErrorHandler(myHandler);
        throw "Error";
    }
}

In Flash 6-8

The Flash VM ignores uncaught exceptions, which complicates debugging. Haxe can compile files with additional exception handling code for catching such exceptions. This feature is enabled by passing the -debug flag to the compiler. Enabling debugging results in slower execution of the generated code and larger SWF files.

Uncaught exceptions (in debug mode) can be reported by setting the error handler, as shown in the following code:

class Test {

    static function myHandler( msg : String, stack : Array<String> ) {
        trace("Error : "+msg);
    }
 
    static function main() {
        flash.Lib.setErrorHandler(myHandler);
        throw "Bye bye";
    }

}

In Flash 9

When using Flash9, always make sure you are using the Debug Player which is part of the free FlexSDK. This will enable you to get dialog boxes when an uncaught exception occurs.

In Neko

When developing server-side application, uncaught exceptions are directly displayed on the web page, with the complete stack trace reporting which functions were called and where the exception occurred. You can't handle uncaught exceptions, the best option is to catch them by having a try...catch block in your main function.

For command-line applications, uncaught exceptions are displayed directly in the command-line console.

Call Stack Information

Stack information can be accessed using the haxe.Stack API. This is supported on all platforms except Flash 9 when the -debug compilation flag is set :

  • callStack() enables you to get information about the current call stack, including the current function.
  • exceptionStack() will return information about the stack of the last caught exception.

For Flash 9, you can call getStackTrace() on an exception object to get a string representation of the call stack, although this will return null unless the flash is played in Adobe's debug player. The trace will include line numbers if the -debug compilation flag is set.

version #19838, modified 2013-12-04 04:05:45 by ReDrUmNZ