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?

suman
  • 718
  • 3
  • 10
  • 26
  • 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. – epascarello Jan 09 '15 at 04:56
  • 1
    the same thing `return` statement is also affected.. Its **Automatic semicolon insertion**. http://stackoverflow.com/a/27738985/3556874 – Naeem Shaikh Jan 09 '15 at 05: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. – suman Jan 09 '15 at 05: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. – epascarello Jan 09 '15 at 05:22

1 Answers1

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.

Community
  • 1
  • 1
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284