Search code examples
c++exceptiontry-catchrethrow

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


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.


Solution

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