0
0
C++programming~10 mins

Try–catch block in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try–catch block
Start
Try block begins
Code runs
Exception thrown?
NoTry block ends normally
|Yes
Catch block matches exception
Catch block runs
Continue after catch
End
The program tries code inside the try block. If an error (exception) happens, it jumps to the matching catch block to handle it, then continues.
Execution Sample
C++
try {
  int x;
  int denom = 0;
  if (denom == 0) {
    throw std::runtime_error("Division by zero");
  }
  x = 10 / denom;
  std::cout << "No error" << std::endl;
} catch (const std::exception& e) {
  std::cout << "Error caught" << std::endl;
}
This code checks for division by zero before dividing, which causes an exception caught by the catch block that prints an error message.
Execution Table
StepActionEvaluationResult
1Enter try blockStart executing code inside tryProceed
2Check for division by zeroDivision by zero detectedException thrown
3Jump to catch blockCatch block matches std::exceptionCatch block runs
4Execute catch blockPrint "Error caught"Output: Error caught
5Exit try-catchContinue normal flowProgram continues
💡 Exception thrown at division by zero, caught by catch block, program continues after handling
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xundefinedException thrown, no value assignedNo changeNo value assigned
Key Moments - 2 Insights
Why does the program jump to the catch block instead of crashing?
Because an exception was thrown inside the try block (see Step 2 in execution_table), the program looks for a matching catch block to handle it (Step 3), preventing a crash.
Does the code after the error inside the try block run?
No, once the exception is thrown (Step 2), the rest of the try block is skipped and control moves to the catch block (Step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at Step 2?
AThe catch block starts running
BThe division by zero causes an exception to be thrown
CThe program prints "No error"
DThe try block finishes normally
💡 Hint
Check the 'Evaluation' and 'Result' columns at Step 2 in the execution_table
At which step does the catch block execute?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Jump to catch block' and 'Catch block runs' in the execution_table
If no exception was thrown, what would happen to the output?
AIt would print "No error"
BIt would print "Error caught"
CIt would print nothing
DThe program would crash
💡 Hint
Think about what happens if the try block finishes without exceptions (see Step 2 and exit_note)
Concept Snapshot
try {
  // code that might throw
} catch (ExceptionType e) {
  // handle error
}

- Code in try runs normally
- If error occurs, jump to matching catch
- Catch handles error, program continues
- Prevents crashes by managing exceptions
Full Transcript
A try-catch block lets your program try some code that might cause an error. If an error happens, the program jumps to the catch block to handle it safely. This way, the program does not crash but continues running after dealing with the problem. In the example, a division by zero check throws an exception caught by the catch block, which prints an error message. The code after the error inside try does not run. The catch block runs only if an exception occurs.