Allowing to omit brackets in try/catch will be useful?

Hi everyone.

I sometimes think that it would be very convenient if I can omit brackets('{' , '}') in try/catch for a single statement. (as just same as if/for/while ...)

Not exactly, but try/catch is somewhat similar with 'if' statement in terms about conditional flow change.

I'm tired of typing {} twice whenever I want to handle some exception.

I'm not sure whether allowing that causes any syntactic ambiguity or serious problem, but if it is theoretically possible, then would it be helpful?

I think so. How about you guys?

Last edited on
Stroustrup discussed this design choice here: http://www.stroustrup.com/except89.pdf - skip to 16.8 Appendix D: Syntax Details
Thank you. Stroustrup pointed out the ambiguity. But he said

"There seems to be no reason to allow these ambiguities even if there is a trivial and systematic way for a parser to chose one interpretation over another. ".

Here's my question. What if it is turns out to be really convenient?

if-else has exactly same problem but is allowed by some arbitrary syntactic rule
https://en.wikipedia.org/wiki/Dangling_else#C
(Here the rule is just attaching 'else' to the nearest if. )

I don't know the historical background over that issue, but I believe that's because if-else appears too frequently and needs some syntactic help.

Why not for try-catch? It's matter of being actually convenient or not. (I'm not sure about that.)
Last edited on

...in try/catch for a single statement...

..I'm tired of typing {} twice whenever I want to handle some exception.


That hints to me that you're may be using exceptions too often for more common error handling instead of exceptional circumstances. If that single statement is a function that does a lot of work, then maybe not, but I don't usually find a great many try/catch clauses.
I agree that it is annoying and contradictory.

But it isn’t going to change. Pick a bigger battle.
I write so few try-catch blocks and I prefer to always write the {} even for if statements so it doesn't really affect me, but it is inconsistent, I give you that. Even switch statements allow you to leave out the {} even though it's not very useful.
Last edited on
By the way, Herb Sutters current proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r2.pdf includes optional syntax that would make this work:
return try “xyzzy”s + “plover”;
and
try return s + “plover”;
and even
1
2
3
class C {
 try string s = “xyzzy”; // (1)
};

I can't predict what's going to happen to that proposal, but at least there are people in the committee unafraid to rock the status quo in this area
Nice!
Topic archived. No new replies allowed.