Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Exception handling


Exceptions are messages passed from the inner call of your program to the outer call (it is going through the stack from the most recent call to the older one). In haXe, any object can be thrown as an exception. Exception handling (that is intercepting those messages) is done with the help of the try and catch keywords.

try
{
   doSomething();
} catch (e : Int)
{
   //If do something throws an Int this block of code will be executed.
} catch (e : String)
{
   //If do something throws a String this block of code will be executed.
} catch (e : Dynamic)
{
   //If do something throws something else this block of code will be executed.
}

As you can see in this example, you can specify different types of exceptions to intercept and execute different blocks of code according to these types. The Dynamic type allows you to intercept any type of exception that hasn't been intercepted before. Mind the order in which you write your blocks, as they are evaluated from top to bottom...