Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ allow to throw anything?

I just saw this question and its related answers. Considering I never ran into this way of using throws before, I was quite surprised to find out this is even possible.

  • What's the logic behind allowing (almost) anything to be thrown and caught?
  • Is there a use of the throw-catch syntax that goes beyond exception/error signaling? And if so, is that considered bad practice or are there "common" usages of this I was never aware of?
like image 677
GPhilo Avatar asked Dec 05 '18 10:12

GPhilo


2 Answers

This is hard to answer without speculation, but Bjarne's 1998 paper "An Overview of the C++ Programming Language" uses arbitrary types when describing exception handling, and suggests creating a hierarchy of said types for convenient/semantics. It would seem that he did not have a "base class" of exception in mind for any of these in the beginning.

It's possible that the notion of having a standard hierarchy (based on std::exception) began life as an "addition", a convenient way to approach Bjarne's suggested use of exceptions, rather than the building-blocks on which everyone's use of exceptions should be based. That contemporary practice is to derive all exceptions from std::exception would seem to have come along later.

Nowadays, I can't think of a good reason not to do that, if for no other reason than people using your code will probably expect a top-level catch (const std::exception&) to suck up anything useful. I do also tend to put a catch (...) in main, though, just in case.

Speaking more practically, if this weren't the case, there would have to be additional rules constraining throw to only be "valid" on expressions with type deriving from std::exception, which would not seem to have any real-world benefit sufficient to justify the additional rules. C++, believe it or not, comes from a place of minimalism in terms of the question "why don't we have such-and-such a rule", although obviously its bloat would seem to contradict this after all these years.

I doubt this has anything to do with using throw for non-exceptional things, because that has always been considered bad practice. The way Bjarne describes this feature is:

Exceptions are used to transfer control from a place where an error is detected to some caller that hasexpressed interest in handling that kind of errors. Clearly, this is a mechanism that should be used only for errors that cannot be handled locally. [..] Exceptions can be used to make error handling more stylized and regular.

So it's clear that at least the original design intent (and, again, this is still common wisdom) is to use exceptions for exceptional/errory cases only.

like image 135
Lightness Races in Orbit Avatar answered Oct 04 '22 14:10

Lightness Races in Orbit


1. You can throw anything because C++ does not define the purpose of specific classes as java or C# do. In this languages you can only throw objects of classes which inherit from their specified exception class. C++ does not have the one exception class(nobody forces you to use std-stuff).

2. Using throw/catch to signal non-error messages is called exception driven development and is indeed considered bad practice. exception driven development leads to code that's hard to understand. It also can result in sections of code not getting executed unintentionally. I highly recomment against doing this.

like image 45
Detonar Avatar answered Oct 04 '22 14:10

Detonar