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

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

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

}<br />

return num1/num2;<br />

}<br />

try {<br />

var theNum:Number = divi<strong>de</strong>Num(1, 0);<br />

trace("SUCCESS! "+theNum);<br />

} catch (e_err:Error) {<br />

trace("ERROR! "+e_err.message);<br />

trace("\t"+e_err.name);<br />

}<br />

Si vous testez ce co<strong>de</strong> <strong>ActionScript</strong> sans modifier les nombres que vous divisez, une erreur<br />

s'affiche dans le panneau <strong>de</strong> sortie car vous essayez <strong>de</strong> diviser par 0.<br />

Voir également<br />

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

name (propriété Error.name)<br />

public name : String<br />

Contient le nom <strong>de</strong> l'objet Error. Par défaut, la valeur <strong>de</strong> cette propriété est "Error".<br />

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

Exemple<br />

Dans l'exemple suivant, une fonction renvoie une erreur spécifiée en fonction <strong>de</strong>s <strong>de</strong>ux<br />

nombres que vous essayez <strong>de</strong> diviser. Ajoutez le co<strong>de</strong> <strong>ActionScript</strong> suivant à l'image 1 <strong>du</strong><br />

scénario :<br />

function divi<strong>de</strong>Number(numerator:Number, <strong>de</strong>nominator:Number):Number {<br />

if (isNaN(numerator) || isNaN(<strong>de</strong>nominator)) {<br />

throw new Error("divi<strong>de</strong>Num function requires two numeric parameters.");<br />

} else if (<strong>de</strong>nominator == 0) {<br />

throw new Divi<strong>de</strong>ByZeroError();<br />

}<br />

return numerator/<strong>de</strong>nominator;<br />

}<br />

try {<br />

var theNum:Number = divi<strong>de</strong>Number(1, 0);<br />

trace("SUCCESS! "+theNum);<br />

// output: Divi<strong>de</strong>ByZeroError -> Unable to divi<strong>de</strong> by zero.<br />

} catch (e_err:Divi<strong>de</strong>ByZeroError) {<br />

// divi<strong>de</strong> by zero error occurred<br />

trace(e_err.name+" -> "+e_err.toString());<br />

} catch (e_err:Error) {<br />

// generic error occurred<br />

trace(e_err.name+" -> "+e_err.toString());<br />

Erreur 567

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

Saved successfully!

Ooh no, something went wrong!