Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
Not all exceptions can be caught. Quoting from \[except.handle\], p13 of [N3000|http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf], the current Working Draft of the C+\+ standard:

Exceptions thrown in destructors of objects with static storage duration or in constructors of namespace-scope objects with static storage duration are not caught by a function-try-block on main(). Exceptions thrown in destructors of objects with thread storage duration or in constructors of namespace-scope objects with thread storage duration are not caught by a function-try-block on the initial function of the thread.

To illustrate using an example:

Code Block
struct Foo {
  Foo();    // may throw
  ~Foo();   // may throw
};

Foo A;

void bar() {
  static Foo B;
}

int main()
try {
  bar();
  // other executable statements
}
catch(...) {
  // will catch exceptions thrown from bar() and
  // any other executable statements in the try
  // block above

  // IMPORTANT: will not catch exceptions thrown
  // from the constructor of the global object A
  // or those from the destructor of the static
  // local object B defined in bar()
}

Thus, it is important to prevent constructors and destructors of objects with static storage duration to throw exceptions.