5

In C++ should I use std::runtime_error to indicate some sort of error occurred, or should I create custom exceptions that inherit from std::runtime_error so I can better handle them.

For example, if I got input from the user somehow, which would be better:

if (inputInvalid)
{
    throw std::runtime_error("Invalid input!");
}

Versus...

class invalid_input
    : public std::runtime_error /* or should I inherit from std::exception? */
{
public:
    invalid_input()
        : std::runtime_error("Invalid input!")
    {
    };
};

-------------------------------------------------------

if (inputInvalid)
{
    throw invalid_input();
}

Which is considered better use of exception handling/which if better practice?

Josh
  • 6,046
  • 11
  • 52
  • 83
  • The latter will make it possible to catch this specific type of runtime error, while the first only allows you to catch 'a' runtime error. Which is better really depends on how you plan to deal with errors. – wroniasty Apr 26 '12 at 15:39
  • This is a very touchy subject, on which everyone has a different opinion. Just look at the `Go`-like approach (`throw` terminates the task, so no need to have distinct types) vs the `Java` approach and its very intricate hierarchies. – Matthieu M. Apr 26 '12 at 15:39

4 Answers4

7

I would only subclass a standard exception class in one of two cases:

  1. None of the standard exception classes have names that describe the nature of my exception
  2. My exception needs additional information than what can be found in a standard exception (i.e. a string describing the error).

Otherwise, there's not much point. Especially if others will have to interact with your code and wonder why there is a custom exception class that doesn't do anything a standard exception doesn't.

suszterpatt
  • 8,187
  • 39
  • 60
1

Always think about what you gain from inheriting std::runtime_error. Does it let you handle the error easier? In the example code it doesn't give any benefits so no there is no point in inheriting from std::runtime_error if all it does is the same thing. If you want to add more information than what std::runtime_error has then you might want to inherit and add those to your error class.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
1

That will depend on the size of the project. If your working on something small and just need something quick and dirty, std:runtime_error is fine. But if your working on a big project you are going to want to create your own custom exceptions to help manage(through catches) all the different possibilities. Otherwise now if you made a catch, you would be catching EVERYTHING, which may be a problem if you need to catch multiple different things with different ways to handle them.

I would also read this:

Difference: std::runtime_error vs std::exception()

Community
  • 1
  • 1
AwDogsGo2Heaven
  • 952
  • 11
  • 26
1

I would subclass std::exception() for your invalid_input class in the example you gave. Typically, a user input error is not considered a runtime error, you may want to catch it earlier, you may want to perform different logic when it occurs. Using or subclassing in this case from runtime_exception in most cases fails the "is a" test.

jhoffmann
  • 31
  • 1