13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Handling errors<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.text.TextField;<br />

}<br />

public class SimpleError ext<strong>en</strong>ds Sprite<br />

{<br />

public var employee:XML =<br />

<br />

1234<br />

1-234<br />

;<br />

}<br />

public function SimpleError()<br />

{<br />

try<br />

{<br />

if (employee.costC<strong>en</strong>ter.l<strong>en</strong>gth() != 1)<br />

{<br />

throw new Error("Error, employee must have exactly one cost c<strong>en</strong>ter assigned.");<br />

}<br />

}<br />

catch (error:Error)<br />

{<br />

var errorMessage:TextField = new TextField();<br />

errorMessage.autoSize = TextFieldAutoSize.LEFT;<br />

errorMessage.textColor = 0xFF0000;<br />

errorMessage.text = error.message;<br />

addChild(errorMessage);<br />

}<br />

}<br />

Using a wider range of error classes and built-in compiler errors, ActionScript 3.0 offers more information than<br />

previous versions of ActionScript about why something has failed. This information <strong>en</strong>ables you to build more stable<br />

applications with better error handling.<br />

Rethrowing errors<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Wh<strong>en</strong> you build applications, there are several occasions in which you need to rethrow an error if you are unable to<br />

handle the error properly. For example, the following code shows a nested try..catch block, which rethrows a<br />

custom ApplicationError if the nested catch block is unable to handle the error:<br />

Last updated 6/6/2012<br />

61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!