Bird
0
0

You want to catch all exceptions except NullReferenceException and handle them differently. Which approach correctly implements this in C#?

hard🚀 Application Q15 of 15
C Sharp (C#) - Exception Handling
You want to catch all exceptions except NullReferenceException and handle them differently. Which approach correctly implements this in C#?
AUse a catch block for Exception and rethrow if NullReferenceException
BUse a single catch block for Exception and check exception type inside
CUse two catch blocks: one for NullReferenceException, one for Exception
DUse a catch block for NullReferenceException only
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want to handle all exceptions except NullReferenceException differently, so we must detect and exclude it.
  2. Step 2: Evaluate approaches

    Use a catch block for Exception and rethrow if NullReferenceException uses a general catch for Exception, then rethrows if the exception is NullReferenceException, effectively excluding it from handling.
  3. Final Answer:

    Use a catch block for Exception and rethrow if NullReferenceException -> Option A
  4. Quick Check:

    Rethrow to exclude specific exceptions [OK]
Quick Trick: Rethrow specific exceptions inside general catch [OK]
Common Mistakes:
MISTAKES
  • Catching NullReferenceException first but not rethrowing
  • Handling all exceptions in one catch without rethrow
  • Using only NullReferenceException catch block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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