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

When clause in catch in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - When clause in catch
Try block starts
Exception thrown?
NoTry block ends normally
Yes
Catch block with when clause
Evaluate when condition
Condition true?
NoSkip this catch
Next catch or unhandled
Execute catch block
Continue after try-catch
When an exception occurs, the catch block with a when clause checks the condition; if true, it runs, else it skips to the next catch.
Execution Sample
C Sharp (C#)
try {
  int x = int.Parse("abc");
} catch (FormatException e) when (e.Message.Contains("Input")) {
  Console.WriteLine("Caught FormatException with 'Input' in message");
}
This code tries to parse a string to int and catches FormatException only if the message contains 'Input'.
Execution Table
StepActionException ThrownWhen Condition EvaluatedCondition ResultCatch Block ExecutedOutput
1Enter try blockNoN/AN/ANo
2int.Parse("abc") throws FormatExceptionFormatExceptionN/AN/ANo
3Catch block with when clause checks conditionFormatExceptione.Message.Contains("Input")TrueYesCaught FormatException with 'Input' in message
4Catch block executed, program continuesNoN/AN/ANo
💡 Catch block executed because when condition was true; program continues normally.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
e.MessageN/A"Input string was not in a correct format.""Input string was not in a correct format.""Input string was not in a correct format."
Key Moments - 2 Insights
Why does the catch block only run when the when condition is true?
Because the when clause acts like a filter; if the condition is false, the catch block is skipped (see execution_table row 3).
What happens if the when condition is false?
The catch block is skipped and the runtime looks for another catch block or the exception remains unhandled (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of the when condition at step 3?
ATrue
BFalse
CNot evaluated
DException message
💡 Hint
Check the 'Condition Result' column at step 3 in the execution_table.
At which step does the catch block actually execute?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Catch Block Executed' column in the execution_table.
If the when condition was false, what would happen next?
AThe catch block runs anyway
BThe exception is ignored
CThe runtime looks for another catch block
DThe program crashes immediately
💡 Hint
Refer to the concept_flow where the false condition leads to skipping the catch block.
Concept Snapshot
try {
  // code that may throw
} catch (ExceptionType e) when (condition) {
  // runs only if condition is true
}

- The when clause filters exceptions
- If false, catch is skipped
- Helps handle exceptions selectively
Full Transcript
This example shows how the when clause in a catch block works in C#. When an exception is thrown inside the try block, the catch block with a when clause checks the condition. If the condition is true, the catch block runs and handles the exception. If false, it skips this catch and looks for another. This lets you handle exceptions more precisely based on extra conditions.