Confused about std::runtime_error vs. std::logic_error
Solution 1:
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.
Solution 2:
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.