Will exception thrown in catch block be caught by later catch blocks?

WilliamKF picture WilliamKF · Aug 6, 2011 · Viewed 11.8k times · Source

Consider the following C++ code:

try {
  throw foo(1);
} catch (foo &err) {
  throw bar(2);
} catch (bar &err) {
  // Will throw of bar(2) be caught here?
}

I would expect the answer is no since it is not inside the try block and I see in another question the answer is no for Java, but want to confirm C++ is also no. Yes, I can run a test program, but I'd like to know the language definition of the behavior in the remote case that my compiler has a bug.

Answer

Puppy picture Puppy · Aug 6, 2011

No. Only exceptions thrown in the associated try block may be caught by a catch block.