在 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
由于以下原因,我对实际使用它们表示怀疑:
- 编译器并没有真正以任何严格的方式强制执行异常说明符,因此好处并不大。理想情况下,您希望得到一个编译错误。
- 如果一个函数违反了异常说明符,我认为标准行为是终止程序。
- 在 VS.Net 中,它将 throw(X) 视为 throw(...),因此对标准的遵守并不强。
你认为应该使用异常说明符吗?
请回答“是”或“否”,并提供一些理由来证明您的回答是正确的。