Challenge - 5 Problems
When Clause Catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of try-catch with when clause filtering
What will be the output of this C# program when the input number is 5?
C Sharp (C#)
using System; class Program { static void Main() { int number = 5; try { if (number < 10) throw new Exception("Small number"); Console.WriteLine("Number is large"); } catch (Exception ex) when (number > 10) { Console.WriteLine("Caught exception for large number"); } catch (Exception ex) when (number < 10) { Console.WriteLine("Caught exception for small number"); } } }
Attempts:
2 left
💡 Hint
Look at the when clauses and which one matches the number 5.
✗ Incorrect
The exception is thrown because number is 5 (less than 10). The first catch has when (number > 10) which is false, so it is skipped. The second catch has when (number < 10) which is true, so it catches and prints 'Caught exception for small number'.
❓ Predict Output
intermediate2:00remaining
Behavior of multiple catch blocks with when clauses
What will this program print when the input number is 15?
C Sharp (C#)
using System; class Program { static void Main() { int number = 15; try { if (number > 10) throw new InvalidOperationException("Too large"); Console.WriteLine("Number is small"); } catch (InvalidOperationException ex) when (number < 10) { Console.WriteLine("Caught invalid operation for small number"); } catch (InvalidOperationException ex) when (number > 10) { Console.WriteLine("Caught invalid operation for large number"); } } }
Attempts:
2 left
💡 Hint
Check which when clause matches the number 15.
✗ Incorrect
The exception is thrown because number is 15 (greater than 10). The first catch has when (number < 10) which is false, so it is skipped. The second catch has when (number > 10) which is true, so it catches and prints 'Caught invalid operation for large number'.
❓ Predict Output
advanced2:00remaining
Effect of when clause on exception filtering
What will be the output of this program?
C Sharp (C#)
using System; class Program { static void Main() { int number = 0; try { number = 10 / 0; } catch (DivideByZeroException ex) when (number == 0) { Console.WriteLine("Divide by zero caught when number is zero"); } catch (DivideByZeroException ex) { Console.WriteLine("Divide by zero caught"); } } }
Attempts:
2 left
💡 Hint
Check the value of number when the exception is thrown and the when clause condition.
✗ Incorrect
The exception is thrown before number is assigned any value other than 0. The when clause checks if number == 0, which is true, so the first catch block runs and prints 'Divide by zero caught when number is zero'.
❓ Predict Output
advanced2:00remaining
When clause with multiple conditions
What will this program print?
C Sharp (C#)
using System; class Program { static void Main() { int number = 7; try { if (number % 2 == 1) throw new Exception("Odd number"); } catch (Exception ex) when (number > 5 && number < 10) { Console.WriteLine("Caught exception for odd number between 5 and 10"); } catch (Exception ex) { Console.WriteLine("Caught exception"); } } }
Attempts:
2 left
💡 Hint
Check the when clause condition and the value of number.
✗ Incorrect
The exception is thrown because number is 7 (odd). The when clause checks if number is greater than 5 and less than 10, which is true, so the first catch block runs and prints 'Caught exception for odd number between 5 and 10'.
🧠 Conceptual
expert2:00remaining
Understanding when clause effect on exception propagation
Consider this code snippet. Which statement is true about the behavior of the when clause in the catch block?
C Sharp (C#)
try { throw new Exception("Error"); } catch (Exception ex) when (false) { Console.WriteLine("This will never run"); } catch (Exception ex) { Console.WriteLine("Caught in general catch"); }
Attempts:
2 left
💡 Hint
Think about how the when clause filters exceptions.
✗ Incorrect
The when clause acts as a filter. If it evaluates to false, the catch block is skipped and the runtime looks for another matching catch block. Here, the first catch is skipped because when(false) is false, so the second catch block catches the exception.