Bird
0
0

Identify the problem in this C# exception handling snippet:

medium📝 Debug Q7 of 15
C Sharp (C#) - Exception Handling
Identify the problem in this C# exception handling snippet:
try { string[] fruits = new string[2]; Console.WriteLine(fruits[3]); } catch (ArgumentOutOfRangeException e) { Console.WriteLine("Error: " + e.Message); } finally { Console.WriteLine("Process complete"); }
AArray size is incorrectly declared
BThe finally block is missing
CThe catch block is catching the wrong exception type
DNo exception will be thrown
Step-by-Step Solution
Solution:
  1. Step 1: Understand the error

    Accessing fruits[3] when array size is 2 causes IndexOutOfRangeException.
  2. Step 2: Check catch block

    The catch block is set to catch ArgumentOutOfRangeException, which is different.
  3. Step 3: Result

    Since the exception type does not match, the exception is unhandled.
  4. Final Answer:

    The catch block is catching the wrong exception type -> Option C
  5. Quick Check:

    Catch must match thrown exception type [OK]
Quick Trick: Catch block must match actual exception type [OK]
Common Mistakes:
MISTAKES
  • Confusing IndexOutOfRangeException with ArgumentOutOfRangeException
  • Assuming finally block is missing
  • Believing no exception occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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