6

I know that using following block, it is able to catch an Exception.

try{
    // Code
}catch(err){
    alert(err.message);
}

Say, I want to catch a NumberFormatException. How can I catch a specific Exception using JavaScript?
(I actually want to catch NumberFormatException, InterruptedException, ConnectException which are well defined in Java)

3
  • 1
    JavaScript is not Java. There is no NumberFormatException in JavaScript, nor InterruptedException, nor ConnectException. JavaScript has as much in common with Java as a manhole has with a man. If you wish to know whether a string is a well-formed number or not, use isNaN() on the result of parseInt() or parseFloat(). Commented Oct 14, 2014 at 10:08
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 14, 2014 at 10:09
  • this thread has more answers stackoverflow.com/questions/1433558/… Commented Jul 13, 2022 at 0:42

1 Answer 1

8

Javascript has no standard way of doing this.

try{
    // Code
}catch(err){
    if(err instanceof SomeException){
        alert(err.message);
    }
}

Firefox has an extension, which I think is nice and should be more standard.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.