2006/04/20 | 关于定制错误
类别(网摘) | 评论(0) | 阅读(19) | 发表于 04:23

摘自: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));
}

0

评论Comments

日志分类
首页[28]
flash[3]
javascript[3]
C#[1]
feeling[5]
网摘[16]