03.05.2013 Views

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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 ActionScript in Frame 1 of the Timeline:<br />

import InvalidEmailAddress;<br />

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

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

throw new InvalidEmailAddress();<br />

}<br />

}<br />

try {<br />

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

}<br />

catch (e) {<br />

this.createTextField("error_txt", this.getNextHighestDepth(), 0, 0, 100,<br />

22);<br />

error_txt.autoSize = true;<br />

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

}<br />

See also<br />

Error<br />

Statements 201

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

Saved successfully!

Ooh no, something went wrong!