Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about std::runtime_error vs. std::logic_error

People also ask

What does std :: Logic_error mean?

std::logic_errorDefines a type of object to be thrown as exception. It reports errors that are a consequence of faulty logic within the program such as violating logical preconditions or class invariants and may be preventable.

In which standard library of C++ is the Runtime_error defined?

std::runtime_error It reports errors that are due to events beyond the scope of the program and can not be easily predicted. Exceptions of type std:runtime_error are thrown by the following standard library components: std::locale::locale and std::locale::combine.


In this case, I think (at least for the most part) you're right and it's wrong. The standard describes logic_error as:

The class logic_error defines the type of objects thrown as exceptions to report errors presumably detectable before the program executes, such as violations of logical preconditions or class invariants.

A command line argument that can't be parsed doesn't seem to fit that very well.

By contrast, it describes runtime_error as:

The class runtime_error defines the type of objects thrown as exceptions to report errors presumably detectable only when the program executes.

That seems to be a better fit.


From a pure standard point of view, you are right. program_options should throw classes derived from either runtime_error or logic_error depending on whether the error is runtime or logical. (I did not review the current code to determine such idea classification for current exceptions).

From a practical standpoint, I have yet to see C++ code that makes useful decisions based on whether exception is logic_error or runtime_error. After all, the only reason you would throw a logic_error as opposed to letting assert file is if you want to try recover somehow, and that's not different from recovery from a runtime error. Personally, I view logic_error vs. runtime_error the same way as checked exceptions in Java -- theoretically nice, but not useful in practice. Which means, that maybe, I'll just make program_options::error derive from exception. That is, when I'll find that 'spare time' everybody keeps talking about.


The current draft of the C++0x Standard says (clause 19.2):

1) In the error model reflected in these classes (i.e. the exception types), errors are divided into two broad categories: logic errors and runtime errors.

2) The distinguishing characteristic of logic errors is that they are due to errors in the internal logic of the program. In theory, they are preventable.

3) By contrast, runtime errors are due to events beyond the scope of the program. They cannot be easily predicted in advance.

Together with the quotes cited in one of the other answers this explains why Boost.ProgramOptions throws a std::logic_error for preventable errors caused by an 'error presumably detectable before the program executes'.