Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the `std::exception`, in its current form, redundant?

Tags:

c++

exception

Usually, when I want to create my own exception, I inherit from std::exception or std::runtime_error.

Is there anything that stops me from creating my own empty "tag-class"?

class out_of_bounds_access {}; // or:
class memory_leak {};

and throw just that?
After all, mostly, it's the class-name that carries information about what went wrong not the members of the exception class.

Ok, so I assumed this is a bad idea, but why? Why is this a bad idea?

P.S. I know there are cases in which "custom-made" exceptions carry information that latter is used to determine the correct approach to solve the problem...
However, if you think about it, cases like that can, very often (not always, but often), be re-done to throw & catch multiple different tag-classes instead of just a single one (with "content").

like image 826
PatrykB Avatar asked Dec 03 '18 12:12

PatrykB


People also ask

What kind of exceptions are used in C++?

Exception handling in C++ consist of three keywords: try , throw and catch : The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error.

Which is used to handle the exceptions in C++?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

What will happen if thrown exception is not handled in C++?

Explanation: As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped).

How do you throw an exception in CPP?

An exception in C++ is thrown by using the throw keyword from inside the try block. The throw keyword allows the programmer to define custom exceptions. Exception handlers in C++ are declared with the catch keyword, which is placed immediately after the try block.


1 Answers

No, nothing stops you from doing this.

However, some code that wants to catch "any exception" will catch const std::exception&, and if your exception type doesn't derive from std::exception then that won't work.

Sure, we can catch ... instead but that's, in my experience, used as a last-ditch "blunt instrument" for avoiding termination due to uncaught exceptions, and can't tell you anything about the exception itself.

Boost exceptions don't derive from std::exception and it's really annoying.

Why not just make all the exceptions part of this standard hierarchy?

If you don't intend to ever let your exception types make get all the way to the top, then there may not be a practical problem here. But, why take the chance? You lose nothing by adding : std::runtime_error or somesuch, and the text string that you'll pass to the base is useful information for the diagnosing programmer.

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

Lightness Races in Orbit