1

How is

    throw
       {
         name: 'type error',
         message: 'provide numeric value'
       };

a incorrect syntax when

    throw{
         name: 'type error',
         message: 'provide numeric value'
       };

Is a correct syntax?

Is it really necessary to attach curly bracket with throw and why?

4
  • The console says it clearly in chrome: Uncaught SyntaxError: Illegal newline after throw It is the line break that is the problem. Add a space and it would work throw { It does not have to be attached. Commented Jan 9, 2015 at 4:56
  • 1
    the same thing return statement is also affected.. Its Automatic semicolon insertion. stackoverflow.com/a/27738985/3556874 Commented Jan 9, 2015 at 5:17
  • @epascarello please read the question once again, I don't think you are getting me.The space given or not given doesn't matter,what matters is that the { should be in the same line. The { bracked attached together works perfectly. Commented Jan 9, 2015 at 5:20
  • I know what the problem is.... The error message that chrome gives was the answer. And there a few things in JS that can not have a line break after them. You found one of them. Commented Jan 9, 2015 at 5:22

1 Answer 1

6

For some reason, a semicolon is always inserted after a lone throw keyword even though you can't legally have a throw statement by itself, so what you get is this:

    throw;
       {
         name: 'type error',
         message: 'provide numeric value'
       };

which results in a syntax error.

For what it's worth, C# (which, like JS, is also based on an ECMA standard) does support throw; statements as a way to rethrow an exception. Perhaps they're futureproofing ECMAScript for support for a similar feature down the road. But this is just conjecture on my part.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.