Home > Article > Backend Development > Why are Exception Specifications in C Function Signatures Considered Poor Practice?
Exception Specifications in Function Signatures
Question:
Why is using the "throw" keyword in a function signature considered poor practice in C ?
Answer:
Using the "throw" keyword in a function signature is generally discouraged. Instead, it is recommended to handle exceptions within the function and propagate them through the return value or an error code.
The exception specification in a function signature specifies the exceptions that a function can throw. However, the compiler cannot enforce this declaration. It is checked at runtime, which is undesirable due to its performance implications.
Furthermore, exception specifications are not well-supported by many compilers. For example, Microsoft Visual C ignores exception specifications, except for "throw()", which it interprets as a guarantee that no exceptions will be thrown. This inconsistency adds to the potential problems caused by using exception specifications.
The above is the detailed content of Why are Exception Specifications in C Function Signatures Considered Poor Practice?. For more information, please follow other related articles on the PHP Chinese website!