Wednesday, April 25, 2012

Javascript is an exception...

Javascript is an exception ... from any reasonable exception handling. The general construct is

try {
  code_throwing_exceptions();
} catch (e) {
  do_something_with_exception(e);
}

Which seems reasonable. But ... how to catch different exceptions? Well ... it seems that there is only one way and that is manual if-s:


try {
  code_throwing_exceptions();
} catch (e) {
  if (e instanceof RangeError) {
    alert('out of bounds');
  } else if (e instanceof TypeError) {
    alert('type problem!');
  } else {
    raise e;
  }
}

No comments:

Post a Comment