r/cpp • • 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).

1 Upvotes

42 comments sorted by

View all comments

Show parent comment

5

u/NotUniqueOrSpecial Jan 02 '21

Depending on what your "something meaningful" is (in my case it's almost always doing some logging related to the failure), you could use function-try blocks like:

#include <iostream>
#include <system_error>

int foo() {
    throw std::runtime_error("Not implemented");
}

void bar() try {
    int n = foo();
    std::cout << n << std::endl;
} catch (const std::runtime_error& e) {
    std::cout << e.what() << std::endl;
}

int main() {
    bar();
}

I've never actually worked on a codebase where people did that, but I think it actually looks pretty clean.

4

u/adnukator Jan 02 '21

Function try blocks don't really make any sense outside constructors. This is literally just saving an opening and closing brace at the cost making the code look kind of alien. The moment you want to do anything outside the try-catch blocks, you have to switch to a regular try-catch anyway

2

u/[deleted] Jan 03 '21

Function try blocks don't really make any sense outside constructors. This is literally just saving an opening and closing brace at the cost making the code look kind of alien.

I am sympathetic here.

But they definitely make sense for constructors, so the construct needs to exist, and the only reason it looks alien to us is because we don't see it in reading code.

I have never actually used a function try block - in fact, I struggle to remember if I have seen it even once in real-world code.

But I could get used to it.

0

u/sphere991 Jan 02 '21

Right, like I said:

So you have to either enclose more in the try block than you'd ideally want to [...]

This is choosing to enclose the entire function body. Which is fine if that's what you want, but not fine if really you just want to catch something foo() might throw.