03.05.2013 Views

ActionScript 2.0 Language Reference - Adobe Help and Support

ActionScript 2.0 Language Reference - Adobe Help and Support

ActionScript 2.0 Language Reference - Adobe Help and Support

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

throw statement<br />

throw expression<br />

Generates, or throws, an error that can be h<strong>and</strong>led, or caught, by a catch{} code block. If an<br />

exception is not caught by a catch block, the string representation of the thrown value is sent<br />

to the Output panel.<br />

Typically, you throw instances of the Error class or its subclasses (see the Example section).<br />

Availability: <strong>ActionScript</strong> 1.0; Flash Player 7<br />

Parameters<br />

expression:Object - An <strong>ActionScript</strong> expression or object.<br />

Example<br />

In this example, a function named checkEmail() checks whether the string that is passed to<br />

it is a properly formatted e-mail address. If the string does not contain an @ symbol, the<br />

function throws an error.<br />

function checkEmail(email:String) {<br />

if (email.indexOf("@") == -1) {<br />

throw new Error("Invalid email address");<br />

}<br />

}<br />

checkEmail("someuser_theirdomain.com");<br />

The following code then calls the checkEmail() function within a try code block. If the<br />

email_txtstring does not contain a valid e-mail address, the error message appears in a text<br />

field (error_txt).<br />

try {<br />

checkEmail("Joe Smith");<br />

}<br />

catch (e) {<br />

error_txt.text = e.toString();<br />

}<br />

In the following example, a subclass of the Error class is thrown. The checkEmail() function<br />

is modified to throw an instance of that subclass.<br />

// Define Error subclass InvalidEmailError // In InvalidEmailError.as:<br />

class InvalidEmailAddress extends Error { var message = "Invalid email<br />

address."; }<br />

In a FLA or AS file, enter the following <strong>ActionScript</strong> in Frame 1 of the Timeline:<br />

import InvalidEmailAddress;<br />

function checkEmail(email:String) {<br />

if (email.indexOf("@") == -1) {<br />

throw new InvalidEmailAddress();<br />

234 <strong>ActionScript</strong> language elements

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

Saved successfully!

Ooh no, something went wrong!