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

When clause in catch in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
When Clause Catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
        }
    }
}
ACaught exception for small number
BCaught exception for large number
CNumber is large
DNo output
Attempts:
2 left
💡 Hint
Look at the when clauses and which one matches the number 5.
Predict Output
intermediate
2: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");
        }
    }
}
ACaught invalid operation for small number
BUnhandled exception
CCaught invalid operation for large number
DNumber is small
Attempts:
2 left
💡 Hint
Check which when clause matches the number 15.
Predict Output
advanced
2: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");
        }
    }
}
ADivide by zero caught when number is zero
BDivide by zero caught
CNo output
DUnhandled exception
Attempts:
2 left
💡 Hint
Check the value of number when the exception is thrown and the when clause condition.
Predict Output
advanced
2: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");
        }
    }
}
ACaught exception
BCaught exception for odd number between 5 and 10
CNo output
DUnhandled exception
Attempts:
2 left
💡 Hint
Check the when clause condition and the value of number.
🧠 Conceptual
expert
2: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");
}
AThe first catch block will catch the exception because the when clause is ignored.
BThe exception will not be caught and will crash the program.
CThe program will not compile because when clause cannot be false.
DThe exception will be caught by the second catch block because the when clause in the first catch evaluates to false.
Attempts:
2 left
💡 Hint
Think about how the when clause filters exceptions.