Bird
Raised Fist0

Given nested try-catch blocks, what happens if an exception is thrown in the inner try and not caught there?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Exception Handling
Given nested try-catch blocks, what happens if an exception is thrown in the inner try and not caught there?
try {
  try {
    throw new Exception();
  } catch (NullReferenceException) {
    Console.WriteLine("Inner catch");
  }
} catch (Exception) {
  Console.WriteLine("Outer catch");
}
AOuter catch block handles the exception
BInner catch block handles the exception
CException is unhandled and program crashes
DBoth catch blocks execute
Step-by-Step Solution
Solution:
  1. Step 1: Analyze inner try-catch

    Exception thrown is generic Exception, but inner catch only catches NullReferenceException, so it does not catch it.
  2. Step 2: Outer catch handles exception

    Since inner catch does not handle it, exception propagates to outer catch which catches Exception.
  3. Final Answer:

    Outer catch block handles the exception -> Option A
  4. Quick Check:

    Uncaught inner exception propagates to outer catch [OK]
Quick Trick: Exception bubbles up if inner catch doesn't match [OK]
Common Mistakes:
MISTAKES
  • Assuming inner catch handles all exceptions
  • Thinking both catch blocks run
  • Ignoring exception type matching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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