MDC says to do this, but also says that they think it’s just a FireFox extension:
live code: http://jsfiddle.net/vQsUX/
try {
throw("InvalidNameException");
}
catch (e if e == "InvalidNameException") {
document.write(e)
}
catch (e if e == "InvalidIdException") {
document.write(e)
}
catch (e if e == "InvalidEmailException") {
document.write(e)
}
How can I do a similar thing that’ll work for all browsers?
Just catch the object and evaluate it in a single
catchblock:Since exceptions are just objects, and you can
throwanything you like, you can always develop your own more elaborate exception structures. Exception objects can have code to run, numeric keys, whatever you need. That said, I’ve never found much use for an elaborate exception mechanism in Javascript; it just doesn’t seem to come up much. Nevertheless: