在C++中,您可以通过使用异常说明符指定函数可能会也可能不会抛出异常.例如:
void foo() throw(); // guaranteed not to throw an exception
void bar() throw(int); // may throw an exception of type int
void baz() throw(...); // may throw an exception of some unspecified type
Run Code Online (Sandbox Code Playgroud)
由于以下因素,我对实际使用它们表示怀疑:
你认为应该使用异常说明符吗?
请回答"是"或"否"并提供一些理由来证明您的答案.
我已经看到至少一个可靠的源(我采用的C++类)建议C++中特定于应用程序的异常类应该继承std::exception.我不清楚这种方法的好处.
在C#中,继承的原因ApplicationException很明确:你得到了一些有用的方法,属性和构造函数,只需要添加或覆盖你需要的东西.有了std::exception这一切似乎你得到的是一个what()覆盖方法,你也可以同样创造自己.
那么,如果有的话,std::exception作为特定于应用程序的异常类的基类有什么好处呢?有没有好的理由不继承std::exception?