0
0
C Sharp (C#)programming~10 mins

Multiple catch blocks in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple catch blocks
Try block starts
Code runs
Exception thrown?
NoTry block ends normally
Yes
Check first catch block type
Matches
Run catch
Catch block ends
Continue after try-catch
The program tries code in the try block. If an error happens, it checks each catch block in order to find one matching the error type and runs it.
Execution Sample
C Sharp (C#)
try {
  int x = int.Parse("abc");
} catch (FormatException) {
  Console.WriteLine("Format error");
} catch (Exception) {
  Console.WriteLine("General error");
}
This code tries to convert a string to a number. If it fails with a format error, it prints "Format error". For other errors, it prints "General error".
Execution Table
StepActionException Thrown?Catch Block CheckedCatch Block Matches?Output
1Enter try block, execute int.Parse("abc")Yes: FormatExceptionFormatException catchYes
2Run FormatException catch blockNoFormat error
3Exit try-catch, continue programNo
💡 Exception FormatException caught by first catch block, program continues after try-catch.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefinedException thrown, x not assignedNot assignedNot assigned
Key Moments - 2 Insights
Why does the first catch block run instead of the second?
Because the exception thrown is FormatException, which matches the first catch block type exactly (see execution_table step 1). The second catch block is for general Exception and is skipped.
What happens if no catch block matches the exception?
The exception is unhandled and the program crashes or moves to a higher-level handler. In this example, all exceptions are caught by the two catch blocks, so this does not happen.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which catch block handles the exception?
AThe first catch block for FormatException
BThe second catch block for Exception
CNo catch block handles it
DBoth catch blocks handle it
💡 Hint
See execution_table row 1 and 2 where FormatException catch block matches and runs.
At which step does the program output "Format error"?
AStep 1
BStep 2
CStep 3
DNo output is produced
💡 Hint
Check execution_table row 2 where the catch block runs and prints output.
If the thrown exception was NullReferenceException, which catch block would run?
AFormatException catch block
BException catch block
CNo catch block
DBoth catch blocks
💡 Hint
NullReferenceException is a general Exception but not FormatException, so second catch block matches.
Concept Snapshot
try {
  // code that may throw
} catch (SpecificException) {
  // handle specific error
} catch (Exception) {
  // handle other errors
}

- Multiple catch blocks checked in order
- First matching catch runs
- Program continues after catch
Full Transcript
This example shows how multiple catch blocks work in C#. The program tries to run code inside the try block. If an error happens, it looks at each catch block in order. If the error type matches the catch block type, that block runs. In the example, parsing "abc" to int throws a FormatException. The first catch block matches this type and runs, printing "Format error". The second catch block is skipped. After the catch block finishes, the program continues normally. If no catch block matches, the program would crash or look for a higher-level handler. This shows how you can handle different errors differently by using multiple catch blocks.