区别:std :: runtime_error vs std :: exception() [英] Difference: std::runtime_error vs std::exception()
问题描述
std :: runtime_error
和 std :: exception
之间有什么区别?什么是适当的使用每个?为什么它们第一次不同?
std :: exception
是其唯一目的是作为异常层次结构中的基类的类。它没有其他用途。换句话说,在概念上,它是一个抽象的类(即使它没有被定义为抽象类在C ++的意义的术语)。
std :: runtime_error
是一个更专门的类,从 std :: exception
下降,各种运行时错误。它有双重目的。它可以被自己抛出,或者它可以作为各种更专门类型的运行时错误异常的基类,例如 std :: range_error
, std :: overflow_error
等你可以定义自己的异常类从 std :: runtime_error
下降,以及你可以定义自己从 std :: exception
。
下降的异常类就像 std :: runtime_error
,标准库包含 std :: logic_error
,也从 std :: exception
/ p>
这种层次结构的意义在于给用户提供使用C ++异常处理机制的全部功能的机会。由于'catch'子句可以捕获多态异常,用户可以编写catch子句,它可以从异常层次结构的特定子树捕获异常类型。例如, catch(std :: runtime_error& e)
将从 std :: runtime_error
子树中捕获所有异常,所有其他的通过(和进一步向上调用堆栈)。
PS设计一个有用的异常类层次结构(让你只捕获你在代码的每一个点上感兴趣的异常类型)是一个非平凡的任务。在标准C ++库中看到的是一种可能的方法,由语言的作者提供给你。如你所见,他们决定将所有异常类型分成运行时错误和逻辑错误,并让您从那里继续使用您自己的异常类型。
更新:可移植性Linux与Windows
由于Loki Astari和unixman83在下面的回答和注释中指出, exception
类的构造函数不会根据C ++标准的任何参数。 Microsoft C ++有一个构造函数在 exception
类中提取参数,但这不是标准。 runtime_error
类在两个平台,Windows和Linux上都有一个构造函数接受参数( char *
)。为了便于移植,最好使用 runtime_error
。
(请记住,不必在Linux上运行,这并不意味着它永远不必在Linux上运行。)
What is the difference between std::runtime_error
and std::exception
? What is the appropriate use for each? Why are they different in the first place?
std::exception
is the class whose only purpose is to serve as the base class in the exception hierarchy. It has no other uses. In other words, conceptually it is an abstract class (even though it is not defined as abstract class in C++ meaning of the term).
std::runtime_error
is a more specialized class, descending from std::exception
, intended to be thrown in case of various runtime errors. It has a dual purpose. It can be thrown by itself, or it can serve as a base class to various even more specialized types of runtime error exceptions, such as std::range_error
, std::overflow_error
etc. You can define your own exception classes descending from std::runtime_error
, as well as you can define your own exception classes descending from std::exception
.
Just like std::runtime_error
, standard library contains std::logic_error
, also descending from std::exception
.
The point of having this hierarchy is to give user the opportunity to use the full power of C++ exception handling mechanism. Since 'catch' clause can catch polymorphic exceptions, the user can write 'catch' clauses that can catch exception types from a specific subtree of the exception hierarchy. For example, catch (std::runtime_error& e)
will catch all exceptions from std::runtime_error
subtree, letting all others to pass through (and fly further up the call stack).
P.S. Designing a useful exception class hierarchy (that would let you catch only the exception types you are interested in at each point of your code) is a non-trivial task. What you see in standard C++ library is one possible approach, offered to you by the authors of the language. As you see, they decided to split all exception types into "runtime errors" and "logic errors" and let you proceed from there with your own exception types. There are, of course, alternative ways to structure that hierarchy, which might be more appropriate in your design.
Update: Portability Linux vs Windows
As Loki Astari and unixman83 noted in their answer and comments below, the constructor of the exception
class does not take any arguments according to C++ standard. Microsoft C++ has a constructor taking arguments in the exception
class, but this is not standard. The runtime_error
class has a constructor taking arguments (char*
) on both platforms, Windows and Linux. To be portable, better use runtime_error
.
(And remember, just because a specification of your project says your code does not have to run on Linux, it does not mean it does never have to run on Linux.)
这篇关于区别:std :: runtime_error vs std :: exception()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!