Can one exception trigger multiple catch blocks?

Viewed 31

I guess the easiest way to ask the question is to see if the statement "Caught Potato Except" would be printed out.

I know that "Caught Vegetable Except" would be printed out, but after that catch block will it continue sequentially and try to see if the other catch blocks would work as well?

class Vegetable_Except {};

class Potato_Except : public Vegetable_Except{};

int func(int n){
 if(n < 0)
  throw Potato_Except();
 cout << "n: " << n << " ";
 return func(--n);
}


int main(int argc, char ** argv) {
 try{
  func(3);
  func(2);
 }
 catch(Vegetable_Except & e){
  cout << "Caught Vegetable Except" << endl;
 }
catch(Potato_Except & e){
cout << "Caught Potato Except" << endl;
}
 cout << "We survived the vegetables" << endl;
    return 0;
}

Code is slightly adapted from UMich F2020 EECS280.

0 Answers
Related