Main Content

You are here:

Try, Catch and Finally

Hello and welcome. Today we shall be leaning about three concepts of Flash, try, catch and finally. These, when used together, represent one way of catching and even suppressing runtime errors, letting you create code blocks that can intercept errors and deal with them appropriately. If errors aren't intercepted and caught the script will terminate.

Try and Catch

We shall ignore finally to begin with, as it is optional, and instead focus on try and catch. All three follow a similar syntax to if and else clauses, with curly brackets enclosing a block of statements:

try {
        // statements
} catch (error:Error) {
        // statements
}

Let's break that down. First we have the word 'try' and a curly bracket. Before the second bracket, we have a bunch of statements. These are the ones we think may cause a runtime error to occur. After an error has occured, the statements within the catch block are done; this is where you should deal with the error, tidying up and so forth. The 'catch' block starts the 'catch' keyword. Each catch block is associated with an Error type that will run if that kind of error is thrown in the 'try'. This type is specified within round brakets, like a function declaration. In this case, it's an 'Error' object, which we are going to call 'error'. This will now catch all types of error, but you can also be a bit more specific and use the object appropriate for the error you think will happen: 'ArgumentError' for example. If more than one type of error may occur, you should use more than one catch, like so:

try {
        // statements
} catch (error:ArgumentError) {
        trace('An argument error has occured');
} catch (error:Error) {
        trace('An error has occured which is not argument related');
}

Note that the general 'Error' error comes last - this is because of how the compiler works: it starts with the first 'catch' block and works forwards. If the type of the error thrown does not match the error type in this, catch block, the next catch block is checked and so on until there is a match. If there are no more catch blocks to check, the error is 'uncaught' and the program terminates.

Within the catch block, you can use the error you have defined to get more information about it, in the form of these properties, listed on the : errorID, message and name.

Finally

Optionally, you can add a 'finally' block after your one or more catch block(s):

try {
        // statements
} catch (error:Error) {
        // statements
} finally{
        // statements
}
This is a block of statements which runs after the try and catch blocks. What's special about the finally block is that it will always run after try and catch, whether there are no errors, errors caught in catch blocks or uncaught errors not handled in any of the catch blocks. The advantage here is that the finally code will execute even if the program is about to terminate due to an unhandled execution. To repeat: the code in the finally block will always execute. Got that? Good.

At the end of the day...

... try/catch/finally blocks do not represent a magic bullet. Errors should not occur in the first place during well-written code, but if they do, you can handle them in a meaningful way. If you catch and error and suppress it, the SWF will stumble on but with unknown fate. Anything could happen...

Still, at the end of this free ActionScript tutorial, you will hopefully know a little more about try, catch and finally. Good going I think!

Harry.

Comments