O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exception Bubbling

In ActionScript, an exception can be thrown anywhere in a program, even on a frame in a timeline script! Given that an exception can be thrown anywhere, how does ActionScript find the corresponding catch block to handle it? And what if no catch block exists? These mysteries are resolved through the magic of exception bubbling. Let's follow along a bubbly ride with ActionScript as it encounters a throw statement in a program. During the following dramatization, ActionScript's musings are shown in code comments.

When a throw statement executes, ActionScript immediately stops normal program flow and looks for an enclosing try block. For example, here's a throw statement:

// ActionScript: Hmm. A throw statement.
// Is there an enclosing try block for it?
throw new Error("Something went wrong");

If the throw statement is enclosed in a try block, ActionScript next tries to find a catch block whose parameter's datatype matches the datatype of the value thrown (in this case, Error):

// ActionScript: Great, I found a try block.
// Is there a matching catch block?
try {
  throw new Error("Something went wrong");
}

If a matching catch block is found, ActionScript transfers program control to that block:

try {
  throw new Error("Something went wrong");
// ActionScript: Found a catch block whose parameter datatype is Error!
//               The hunt's over. I'll execute this catch block now...
} catch (e:Error) {
  // Handle problems...
}

But if a matching catch block cannot be found or if the throw ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required