Bird
0
0

Consider this code:

hard🚀 Application Q9 of 15
C Sharp (C#) - Exception Handling
Consider this code:
try {
  throw new NullReferenceException();
} catch (Exception ex) when (ex is SystemException) {
  Console.WriteLine("System exception caught");
} catch (Exception) {
  Console.WriteLine("General exception caught");
}

What will be printed?
ASystem exception caught
BGeneral exception caught
CNo output
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Identify thrown exception and filter

    The thrown exception is NullReferenceException, which inherits from SystemException.
  2. Step 2: Evaluate catch with filter

    The first catch block uses a filter to catch exceptions that are SystemException or derived. It matches and prints "System exception caught".
  3. Final Answer:

    System exception caught -> Option A
  4. Quick Check:

    Exception filters catch derived types correctly [OK]
Quick Trick: Exception filters can catch specific subclasses [OK]
Common Mistakes:
MISTAKES
  • Ignoring exception filters
  • Assuming general catch runs first

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes