29

I am reading up on JavaScript exceptions:

You can nest one or more try...catch statements. If an inner try...catch statement does not have a catch block, the enclosing try...catch statement's catch block is checked for a match.

I think I understood it correctly and wrote this test code as follows:

try {

    try {
        throw "error";
    }

} catch( e ) {

    console.log( e );

}

But got this error:

Uncaught SyntaxError: Missing catch or finally after try

I know it clearly says that I am missing a catch or finally but the JavaScript documentation says my code should be valid or am I misunderstanding?

5
  • 5
    You do realize that a try {} block without either a catch of a finally wouldn't actually accomplish anything even if it was allowed since the only reason to put something in a try block is so you can handle an exception. It makes sense this is an error. Commented Mar 8, 2015 at 17:31
  • Yes, I know. I was just testing it out and thought I was misunderstanding the docs so I consulted SO for an explanation an the link to this helped out more than the MDN guide: ecma-international.org/ecma-262/5.1/#sec-12.14 Commented Mar 8, 2015 at 17:41
  • @jfriend00 You can have write try { /* something */} finally {} and it would run. I also think that this would change the behavior of error handling. So this is more like javascript trying to teach programmers how to write code - how does that make sense? Commented Jan 24, 2024 at 15:21
  • 1
    @Rolf - Since a try doesn't do anything without having a catch or finally, it makes sense that the language would prohibit just a try block all by itself. Yes, you can put an empty finally {} if you really want to. That at least meets the language syntax rules. Commented Jan 24, 2024 at 18:27
  • @jfriend00 You know you can write a line such as 42;, it does nothing yet is valid JS. Anyway I can understand the process through which such a syntax would be mandatory. Commented Jan 25, 2024 at 22:02

4 Answers 4

39

The quoted text is quite misleading because it says "if an inner try..catch doesn't have a catch block" which doesn't make any sense. It should just be "if an inner try doesn't have...".

In JavaScript, you can't just have a try on its own; it has to have a catch, finally, or both. So the scenario that quote is referring so isa try/catch containing a try/finally (not another try/catch):

try {
    try {
        throw "error";
    }
    finally {
    }
} catch( e ) {
    console.log( e );
}
Sign up to request clarification or add additional context in comments.

5 Comments

The spec says: "The try...catch statement consists of a try block, which contains one or more statements, and zero or more catch blocks", but where does it say anything about zero catch blocks ==> a finally block is required?
@BaseZen: In §12.14, where it shows the valid list of try statements, which are "try Block Catch", "try Block Finally", and "try Block Catch Finally" (note the absense of simply "try Block").
I think somebody should fix the misleading documentation/guide.
@samyismyhero: MDN is collaboratively-edited by end users, and while usually very good, this means it's sometimes just plain wrong (sometimes in much worse ways than this slightly poor phrasing). (It also means you can fix it if you like, you just need a Mozilla account.)
You know it's funny I was reading documentation on the same MDN on a different subject and they were linking to SO as a form of guidance.
5

It is specified here: (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/try...catch)

The try statement consists of a try block, which contains one or more statements, and at least one catch clause or a finally clause, or both. That is, there are three forms of the try statement:

Comments

4
try {
    try {
        throw "error1";
    } catch (e1){
      console.log(e1)
    }
    throw "error2";
} catch( e2 ) {
    console.log( e2 );
}

>>> error1
>>> error2

In other words, the inner try-catch does not get picked up by the outer try-catch.

However, if you forget the inner catch, and close the inner try-catch with a 'finally' instead, your statement is valid and the inner error is passed on to the outer try-catch. I struggle to see why one would every do that in reality unless it's by accident. If you try something, your intention is to catch the error case I think.

    try {
        try {
            throw "error1";
        } finally  {
          console.log('final stuff')
        }
        throw "error2";
    } catch( e2 ) {
        console.log( e2 );
    }

>>> final stuff
>>> error 1

Comments

0

According to the same source you are referencing, it mentions this:

If an inner try block does not have a corresponding catch block:

  1. it must contain a finally block, and
  2. the enclosing try...catch statement's catch block is checked for a match.

It seems to be that your code is missing a finally block since you chose to omit out the catch block.

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.