开发者

C++: What is the appropriate use for the std::logic_error exception?

开发者 https://www.devze.com 2022-12-21 00:05 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消