Douglas Crockford は、次のようなことを推奨しています。
throw {
name: "System Error",
message: "Something horrible happened."
};
しかし、次のようなこともできます。
function IllegalArgumentException(message) {
this.message = message;
}
throw new IllegalArgumentException("Argument cannot be less than zero");
そして、次のようにします。
try {
//some code that generates exceptions
} catch(e) {
if(e instanceof IllegalArgumentException) {
//handle this
} else if(e instanceof SomeOtherTypeOfException) {
//handle this
}
}
type
Crockford の実装にプロパティを含めて、 instanceof
. 一方を他方に対して行うことには利点がありますか?