Despite the availability of exception specifiers in C , their use is subject to debate. Some developers argue against their inclusion due to certain limitations and drawbacks.
Compiler Enforcement and Lack of Compile Errors
Exception specifiers do not provide rigorous compiler enforcement. While they indicate the intention of a function to throw or not, the compiler does not enforce these specifications strictly. This lack of error handling can result in runtime exceptions despite the exception specifier prohibiting them.
Program Termination on Exception Violation
If a function violates its exception specifier, the default behavior in the standard is often program termination. This drastic action can be undesirable, especially in applications where graceful error handling is crucial.
Inconsistent Interpretation in Visual Studio
Visual Studio treats throw(X) as throw(...), compromising adherence to the standard. This inconsistency can lead to confusion and unexpected behavior.
Criticisms Beyond Technical Issues
Aside from the technical concerns:
Conclusion
Despite the ability to declare exception specifications, their use can lead to problems related to compiler enforcement, program termination, and limitations in certain scenarios. Therefore, the consensus among many developers is that exception specifiers should not be used in C . Instead, adherence to best practices such as returning error codes and catching exceptions appropriately provides a more robust and extensible approach to error handling.
The above is the detailed content of Should Exception Specifiers Be Used in C ?. For more information, please follow other related articles on the PHP Chinese website!