摘自:http://www.darronschall.com/weblog/archives/000076.cfm
try {
someConverstionFunction(string);
} catch (an IllegalArgumentException) {
// arguments passed to function not valid
} catch (a NumberFormatException) {
// string could not be converted to number
} catch (some other Exception) {
// any other Exceptions caught here
}
///////////////////////////////////////////////////////////
IllegalArgumentException.as
class IllegalArgumentException extends Error {
public function IllegalArgumentException () {
super.constructor.apply(this, arguments);
}
}
//////////////////////////////////////
function onlyAllow1Param(param) {
if (arguments.length > 1) {
throw new IllegalArgumentException("Only 1 param allowed");
}
if (param == "cow") {
throw new Error("No cows allowed here");
}
// only 1 param and it's valid, this is where we'd actually do something
// useful...
}
try {
onlyAllow1Param("one", "two");
// test with the above line, then test by replacing
// the above line with: onlyAllow1Param("cow");
// then test one more time with: onlyAllow1Param("something");
// .. notice the 3 different results in the output window?
} catch (iae:IllegalArgumentException) {
trace("IllegalArgumentException caught: " + iae);
} catch (e:Error) {
trace("Error caught: " + e);
} finally {
trace("Finally block executed");
}
///////////////////////////////////////////
try {
throw "this is an error";
} catch (e:Error) {
trace("Error caught. e instanceof Error = " + (e instanceof Error));
trace(typeof(e));
}
回复Comments
作者:
{commentrecontent}