03.07.2013 Views

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

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

if (email.in<strong>de</strong>xOf("@") == -1) {<br />

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

}<br />

}<br />

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

Le co<strong>de</strong> suivant appelle ensuite la fonction checkEmail() dans un bloc <strong>de</strong> co<strong>de</strong> try. Si la<br />

chaîne email_txt ne contient pas une adresse <strong>de</strong> messagerie vali<strong>de</strong>, le message d'erreur<br />

apparaît dans un champ <strong>de</strong> texte (error_txt).<br />

try {<br />

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

}<br />

catch (e) {<br />

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

}<br />

Dans l'exemple suivant, une sous-classe <strong>de</strong> la classe Error est renvoyée. La fonction<br />

checkEmail() est modifiée pour renvoyer une occurrence <strong>de</strong> cette sous-classe.<br />

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

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

address."; }<br />

Dans un fichier FLA ou AS, entrez le co<strong>de</strong> <strong>ActionScript</strong> suivant dans l'image 1 <strong>du</strong> scénario :<br />

import InvalidEmailAddress;<br />

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

if (email.in<strong>de</strong>xOf("@") == -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 />

Voir également<br />

Erreur<br />

Instruction try..catch..finally<br />

try {<br />

// ... try block ...<br />

} finally {<br />

Instructions 235

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

Saved successfully!

Ooh no, something went wrong!