You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Not all exceptions can be caught. Quoting from [except.handle], p13 of N3000, 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:

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.

  • No labels