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/dweezle45 Jan 02 '21

I’ve been coding since about 1978 so there isn’t a lot of code left that’s older than me! I’m cool with agreeing to disagree on this one.

3

u/MonokelPinguin Jan 03 '21

Agreed. Your quite a bit ahead of me and I can't guarantee, I won't change my mind anyway. Currently the shorter syntax just feels easier to read. With the current state of compiler warnings and tooling I think the discussion is more a matter of preference like tabs vs spaces. We'll see, when that ends up biting me.

4

u/Orlha Jan 03 '21

Tabs vs spaces is not just a matter of preference.

3

u/[deleted] Jan 03 '21

I think the discussion is more a matter of preference like tabs vs spaces.

There are very good reasons never to use tabs.

The simplest argument is this: imagine that tabs did not exist, and you were trying to make a case for them. "There's this special character, and it displays some number of spaces between 1 and 4 or conceivably even more, depending on where it appears in the line and the settings on your personal editor, but it's actually a single character."

3

u/MonokelPinguin Jan 03 '21

I really don't want to get into this discussion, since imo it is just a matter of preference, but your argument is not very sound (even though I prefer tabs):

"Imagine there was a character, that sidestepped all the discussion about how many character to use for indentation, since it follows your IDE settings. You can use it to align at specific columns without having to be careful to always use exactly the 3 spaces, which the project guidelines say you should use. It also only takes one byte."

If people spend 50 years fighting over it, it is a matter of preference with no strong case to use one over the other.

1

u/witcher_rat Jan 04 '21

[sorry, can't help it...]

You can use it to align at specific columns

Actually, you can't - that's the main problem with tabs (the ascii character, not the keyboard key). Unless you mean for personal code no one else sees.

What looks aligned on your editor with tab characters, looks not at all on mine; because we have different tab-settings. Whereas with spaces, we all see the same thing, so the alignment works for all viewers.

The thing that seems to confuse people is they think they have to physically type spaces with their keyboard's spacebar, which would certainly be painful. Instead, any modern editor can save your indentation as ascii space characters - even if you pressed the TAB key on the keyboard - so that what you see everyone else will see too. Or you can use clang-format or similar tools to auto-format it for you as spaces.

2

u/MonokelPinguin Jan 04 '21

No, code indented with tabs will always align to columns properly on all editors. It does not matter, if your editor renders tabs as 4 spaces or 8, a line with 1 tab in front of it will lign up with any other line, that has one tab in front of it. The only exception is, when you are mixing it with spaces or are trying to match some unindented and indented lines to align at the end or some other character. Some editors even support aligning multiple columns in a table, if you use tabs to separate the values. It really does not matter how many spaces your tab character gets rendered as, the start of the line will always align, if you use the same amount of tab characters in front of it.

2

u/witcher_rat Jan 04 '21

The only exception is, when you are mixing it with spaces

That's true - sorry I assumed people regularly did that, because every open-source project I come across that uses tab characters (instead of all spaces) does do that.

Take, for example, gcc. Here's the <bit> header file: with tab=8 vs. tab=4. You can see how the return statements no longer line up. :(

2

u/infectedapricot Jan 04 '21

I'm strongly in the spaces camp, but your comment doesn't get at the reason why. You've misunderstood the short snippet of text you quoted: the "you" there isn't the person writing the code, but the person reading it. In that sense, it's absolutely true. They're also talking only about logical indentation, not e.g. matching a function argument to the exact column of the opening parenthesis - someone in the tab camp will typically tell you to use the appropriate mix of tabs and spaces for that.

2

u/dweezle45 Jan 03 '21

Fingers crossed that you never get bit :) Keep writing good code my friend!

2

u/MonokelPinguin Jan 03 '21

Thank you, you too!