0
0
C++programming~10 mins

Multiple catch blocks in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple catch blocks
Try block starts
Exception thrown?
NoTry block ends normally
Yes
Check first catch block
Matches exception type?
NoCheck next catch block
Yes
Execute catch block
Continue after try-catch
The program tries code inside try. If an exception happens, it checks each catch block in order to find a matching type and runs that catch block.
Execution Sample
C++
try {
  throw 10;
} catch (int e) {
  std::cout << "Caught int: " << e;
} catch (...) {
  std::cout << "Caught unknown";
}
This code throws an int exception and catches it with the matching int catch block.
Execution Table
StepActionException TypeCatch Block CheckedMatch?Output
1Enter try blockNoneNoneN/A
2Throw exceptionint (10)First catch (int e)YesCaught int: 10
3Catch block executedint (10)First catch (int e)YesCaught int: 10
4Skip other catch blocksint (10)Second catch (...)No
5Continue after try-catchNoneNoneN/A
💡 Exception matched first catch block (int), so no further catch blocks checked.
Variable Tracker
VariableStartAfter throwAfter catchFinal
eundefined10 (thrown)10 (caught)10 (caught)
Key Moments - 2 Insights
Why does the first catch block run and not the second?
Because the thrown exception is an int, it matches the first catch block's type int (see execution_table step 2). The second catch block is for any type (...), but it is skipped once a match is found.
What happens if no catch block matches the exception?
If no catch block matches, the exception is not handled here and will propagate up. In the table, if no 'Match?' is Yes, the program would terminate or look for another handler.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which catch block handles the exception at step 2?
AFirst catch block (int e)
BSecond catch block (...)
CNo catch block handles it
DBoth catch blocks handle it
💡 Hint
Check the 'Catch Block Checked' and 'Match?' columns at step 2 in the execution_table.
At which step does the program continue after handling the exception?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look for the step labeled 'Continue after try-catch' in the execution_table.
If the thrown exception was a string instead of int, which catch block would handle it?
AFirst catch block (int e)
BNo catch block
CSecond catch block (...)
DBoth catch blocks
💡 Hint
The first catch only matches int, the second catch (...) matches any type (see concept_flow).
Concept Snapshot
try {
  // code that may throw
} catch (Type1 e) {
  // handle Type1 exceptions
} catch (Type2 e) {
  // handle Type2 exceptions
} catch (...) {
  // handle any exception
}

- Multiple catch blocks checked in order
- First matching catch runs
- Others skipped after match
Full Transcript
In C++, you can use multiple catch blocks after a try block to handle different types of exceptions. When an exception is thrown inside the try block, the program checks each catch block in order to find one whose type matches the thrown exception. The first matching catch block runs, and the others are skipped. If no catch block matches, the exception propagates further. This example throws an int exception and catches it with the first catch block that handles int. The second catch block with ... is a catch-all but is not reached here. Variables like the exception object 'e' get assigned the thrown value when caught. This flow helps handle different errors differently in your program.