4 ms·
> What is the reason for allowing returns in try-catch-finally blocks? I think it just makes for a simpler language spec, and in this case, return is treated l
by dvt 5y ago
> What is the reason for allowing returns in try-catch-finally blocks?
I think it just makes for a simpler language spec, and in this case, return is treated like any other statement; then again, there's a note here specifically about try-catch-finally blocks[1] so maybe it doesn't make for a simpler language spec :)
[1] https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-return-statement https://tc39.es/ecma262/multipage/ecmascript-language-statem...
- dahart 5y agoYeah, that’s my thinking - it seems simple at first, but when you see that return and finally when used together are in conflict with their stated / expected behaviors and that the language specifies the tie breaker in a way that might be unexpected... wouldn’t it potentially be simpler both in the spec and to a developer to make return in try-catch-finally blocks a syntax error? I’m assuming there’s a reason to allow the return statements in try-catch-finally blocks, but I can’t think of a use-case. The only use for a return inside a finally block would be to cancel a return in a try or catch block, right? In what cases is that actually desirable? I can see wanting to return from catch, because something bad happened. Overriding that return in finally doesn’t make a lot of sense to me, but I’m certain the ES designers thought about it more than I did.
- jchw 5y agoI actually don’t think it’s to make the language simpler, but most likely for compatibility. First, Statement/Block are parameterized with whether or not they can contain Return statements in the spec, so it wouldn’t complicate the spec much. Second, it is exceedingly rare that JS compatibility breaks. The one recent break I am aware of is: var let = [] let[0] = 1 // Syntax error because let [ conflicts with destructuring lexical declaration.