C++: What is the appropriate use for the std::logic_error exception?
If you use std::logic_error exception in your code, in what case do you u开发者_JAVA百科se it for?
logic_error
is the base for these exceptions:
domain_error
, invalid_argument
, length_error
, out_of_range
.
Those are all logical errors: Somethings wrong with the input such that the output would be illogical. So I'd say you usually don't need to use it directly, since those four cover any logic errors I can think of. But those give you an idea of what the category is.
As GMan already pointed out, it's primarily a base for other exception classes. You might consider using it directly for something that's basically an assertion. E.g. if some code depends on a particular object having been constructed before it executes, it might be appropriate for it to throw a logic_error
(rather than a derivative) if it executes and that object hasn't been constructed yet.
精彩评论