-2

I am so confused and fed up with C++: As I have been taught "passing by reference or pointer" overcomes getting copies of parameters passed in a function.

So in an exception-handling scenario according to the law of thumb: throw by value, catch by reference.

Why Stroustrup in his "A Tour of C++" catches by value? eg:

void test()
{
    try {
        std::vector v(−27);
    }
    catch (std::length_error) {
        // handle negative size
    }
    catch (std::bad_alloc) {
        // handle memory exhaustion
    }
} 

This example is copied from the book. Page 59. (Invariants).

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
WonFeiHong
  • 445
  • 4
  • 16
  • In what way is that "inconsistent"? – Oliver Charlesworth Dec 30 '17 at 19:26
  • @OliverCharlesworth: I am really fed up. I probably didn't use the precise term. – WonFeiHong Dec 30 '17 at 19:28
  • 3
    How is allowing something *new* being inconsistent? What code is going to break as a result? And Bjarne may make mistakes or have another point to make. We program machines, we aren't machines ourselves. – StoryTeller - Unslander Monica Dec 30 '17 at 19:28
  • @StoryTeller: Do you mean a mistake in the book? – WonFeiHong Dec 30 '17 at 19:29
  • 3
    Yes. The concept of an [errata](http://www.stroustrup.com/Tour_printing2.html) exists because authors may make mistakes. Besides, in this case, he only cares about the exception type, not the exception object itself. It's not the end of the world to catch by value. It's not overly *good*, but it's hardly something to get "fed up" with. – StoryTeller - Unslander Monica Dec 30 '17 at 19:31
  • 2
    The purpose of changing the language is to make the language better (for some subjective definition of better). Not everyone agrees that all the changes succeed in doing that, but that is the goal nonetheless. – Benjamin Lindley Dec 30 '17 at 19:31
  • Thank you all guys. I do really appreciate your useful help especially when I get stuck and lost. – WonFeiHong Dec 30 '17 at 19:41
  • Note that `std::bad_alloc` has no data members and thus is extremely cheap to copy. And that it hardly ever is thrown. – Bo Persson Dec 30 '17 at 22:26

1 Answers1

4

Why Stroustrup in his "A Tour of C++" catches by value?

Unless you want to copy the exception for some reason (e.g. store a custom exception type), you should always catch by const&. This might have been an oversight from Bjarne, or in that particular snippet it didn't really matter whether or not the exception was copied.

Related:


Why C++ is changing inconsistently? I mean from time to time everything changes. eg: in C++ 11:

There is no inconsistency here - static_assert merely became more flexible, as it doesn't force the user to provide an error message anymore. This is useful for cases where the boolean condition itself is self-explanatory and an error message would not add anything useful.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • How can a beginner like me to spot those "Errata" of the Designer of the language? – WonFeiHong Dec 30 '17 at 19:33
  • 1
    @WonFeiHong: posting on StackOverflow is a good start. C++ is a very big and complicated language, and everyone makes mistakes... In this case, the mistake/oversight is not really problematic - just keep in mind that catching by `const&` should always be the first choice unless you really want to copy an exception for some reason. – Vittorio Romeo Dec 30 '17 at 19:36
  • 3
    @WonFeiHong, there is no single designer. Subscribe to standardization comittee mailing. IIRC LWG issues get posted there. – Incomputable Dec 30 '17 at 19:37
  • 1
    Thank you too much! Yes indeed in my examples I catch by `const&`. Thanks for making it so clear. Also here on S.O I really get a very useful help. – WonFeiHong Dec 30 '17 at 19:38
  • @Incomputable: Thank you. – WonFeiHong Dec 30 '17 at 19:39