Feature request for JavaScript: omit catch

1 points by calkuta ↗ HN
I propose that statements of the form:

try {

    // do stuff
}

// do more stuff

be valid.

Further I propose that we may emit an argument to throw, e.g.

throw;

8 comments

[ 3.6 ms ] story [ 27.4 ms ] thread
(comment deleted)
What happens if something is thrown? You can have try without catch if there is a finally block, but with neither catch nor finally, then what?

(There are other features I wanted in JavaScript too, though, such as getting rid of automatic semicolon insertion, and adding a "goto" command, and adding macros (these three things can all be implemented using a preprocessor, so even if they are not a part of JavaScript, they can still be used). There are also some things that can't easily or efficiently be added other than adding into JavaScript directly, such as new functions for integers such as popcount and ctz and Date.bigNow() and the MOR and MXOR of MMIX. Some things they have already added that I have wanted, such as integers and byte arrays; I am glad that they added those things.)

Errors are silently suppressed, equivalent to

try {

} catch (err) { }

This is contrary to what normally happens when you have try/finally without catch; in that case, it is rethrown automatically. That would be confusing if it was different, I think, and worthless if it was the same (being doing nothing).
Since

  try {
    ...
  } catch(err) {}
does that, is clear and explicit, and doesn’t make no-catch mean something different than it does in

  try {
    ...
  } finally {
    ...
  }
I think “no 'catch' as empty 'catch' when there is no 'finally'” is just unnecessary inconsistency for no meaningful gain.
Also, if the argument to throw is omitted, then what value is thrown? Would it throw undefined?
Actually, I suppose if the throw with no argument occurs in a catch block, then it can just rethrow whatever was caught (you might omit giving a name to the variable with the exception if the only thing you will do with it is to rethrow it); in other contexts, it could throw undefined.
try without either catch or finally makes no sense.

Omitting (not emitting) an argument to throw would make sense for rethrowing errors in a catch block (many languages already do this), so it may be a good idea.