Should I Incorporate Exception Specifiers in C ?
Exception specifiers in C allow functions to indicate whether they may throw exceptions, such as:
<code class="cpp">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</code>
While these specifiers can convey intent, their practical use is questionable due to several factors:
Limitations in Enforcement
Compilers do not strictly enforce exception specifiers, reducing their effectiveness. Ideal behavior would involve compile errors for violating specifications, but this is not guaranteed.
Severe Punishment for Violations
If a function violates an exception specifier, the standard behavior is to terminate the program. This harsh response may not be desirable in all cases.
Inconsistent Treatment in Development Environments
Some development environments, such as VS.Net, treat throw(X) as throw(...), undermining adherence to the standard.
Arguments Against Exception Specifiers
In addition to the above concerns, several arguments advocate against the use of exception specifiers:
When Exception Specifiers May Be Useful
Despite the general recommendation against using exception specifiers, they can still be beneficial when:
The above is the detailed content of Are Exception Specifiers Still Relevant in Modern C ?. For more information, please follow other related articles on the PHP Chinese website!