Search code examples
c++exceptiontry-catchexception-safety

Ensuring that Exceptions are always caught


Exceptions in C++ don't need to be caught (no compile time errors) by the calling function. So it's up to the developer's judgment whether to catch them using try/catch (unlike in Java).

Is there a way one can ensure that the exceptions thrown are always caught using try/catch by the calling function?


Solution

  • No.

    See A Pragmatic Look at Exception Specifications for reasons why not.

    The only way you can "help" this is to document the exceptions your function can throw, say as a comment in the header file declaring it. This is not enforced by the compiler or anything. Use code reviews for that purpose.