How can I guess all the possible exceptions that can be thrown in C ++?

I am including a third party header file. It has functions that may / may not throw exceptions. In my source code, how do I determine which exceptions can be selected from this file? It was an interview question. My answer went through function declarations and looked for an exception specification. This may give us some hint. Is there any other way by which we can predict exceptions that could be thrown?

+5
source share
1 answer

This has several aspects:

  • If exceptions are declared for a function, only those (and derived classes, of course) can be thrown, all others will cause program termination.
  • Even if exceptions are declared for a function, not every compiler actually applies this rule.
  • If no exceptions are declared for the function (and not an empty set of exceptions!), Everything can be thrown.
  • Sensible code will never throw anything not derived from std::exception , so assuming either a derived type is thrown is a good approach.
  • Good code will document error handling, although you should implicitly expect std::bad_alloc from any memory allocation function.
+3
source

All Articles