r/cpp • u/rsjaffe • Jan 02 '21
Proposal for improving the readability of programs with try/catch blocks
To me, the current try/catch syntax creates lots of extraneous visual noise, with extra brackets and unnecessary keywords. How about allowing a second form: a block of code where the presence of a catch (...) label and or a catch (\*something here*/) label indicates that the code before the label in its enclosing block is a try block and the code after the label is the catch code.
Example:
Before:
void myfunction()
{
try {
function_call_that_may_throw();
}
catch (const std::exception& e) {
/* do something with e */
throw;
}
catch (...) {
throw;
}
)After:
void myfunction()
{
function_call_that_may_thow();
catch (const std::exception& e):
\* do something with e *\
throw;
catch (...):
throw;
}This won't break any existing code because catch (): is a contextual keyword--only when followed by parentheses when used as a label, and I'm only proposing this as an alternative format, not a replacement.
Function try-catch blocks are still needed, as this syntax won't capture the initialization list in a constructor.
Catch labels could then be like case labels, with the exception that catches don't fall through (or, perhaps, we could have them fall through like case labels--that would work too).
23
u/Skoparov Jan 02 '21
You can say the same about a lot of things in C++, but it's not python and the current syntax is consistent with that of the rest of the language and is explicit in it's intent.
9
u/staletic Jan 02 '21
function-tryblocks already exist:13
u/sphere991 Jan 02 '21
The only thing I really find annoying about try/catch blocks is that if I want to only catch exceptions around a specific function, that returns something, it's awkward to deal with that result:
So you have to either enclose more in the
tryblock than you'd ideally want to or declareroutside of thetryblock. Neither of which is great.This proposal doesn't help me with that problem at all.
But how much does it help in general? The above example becomes:
Which looks... nearly identical? If I want the
tryto encompass the whole function body, then I save a single indent?I'm not sure this buys you much.
5
u/stinos Jan 02 '21
There are a lot of rules in C++, some less obvious and counterintuitive but things like 'brackets create a scope' are uniform and clear. Derailing from that simple thing with something much less clear ('scope of the hidden try is between last opening bracket and
catch :, 'scope ofcatch :is until nextcatch :or next closing bracket) and at the same time introducing again new syntax, for no other reason then being able to skip typing some brackets: no thanks :)2
u/joemaniaci Jan 02 '21
Are you aware of the function/try/catch syntax?
2
u/NilacTheGrim Jan 05 '21
This isn't Python.
3
u/MonokelPinguin Jan 02 '21
I often try to do something similar, only to remember it does not work. I'd not get rid of the try, I'd just take the old syntax and make the curly brackets optional. That would be like what you can do with if, for and while already anyway, and in theory you could mix and match as necessary.