Why is the syntax for the Try Block statement try { statements } [catch (exception) { statements }] [finally { statements }]?
Why isn't the syntax try statement [catch (exception) statement] [finally statement]
like a lot of other statements?
What was the reasoning behind forcing the Try Block to be a block statement, when the specification was written?
try
was introduced in ES3 so it's hard to say for certain, but I imagine one of the largest motivations must have been to follow prior art like C++ and Java. (Turning the question on its head, one could also ask "why does for
not require braces?" and the answer then would surely be "because of C".)
Given that braceless try
is currently a syntax error, it should be web compatible to propose it (much like ES2019's optional catch binding feature)! The challenge would simply be to convince folks of its desirability (in a world where, say, Rust has gone in the opposite direction and required braces for all control flow constructs).