Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Exception Handling
Identify the error in this C# code snippet:
try {
    int[] arr = new int[2];
    arr[3] = 10;
} catch (Exception e) {
    Console.WriteLine("Exception caught");
}
AArray index out of bounds exception is not caught.
BThe catch block syntax is incorrect.
CThe code will throw an exception but catch block handles it correctly.
DThe try block has a syntax error.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block code

    Accessing index 3 in an array of size 2 causes an IndexOutOfRangeException.
  2. Step 2: Check catch block handling

    Catch block catches all exceptions of type Exception, so it will handle this error and print the message.
  3. Final Answer:

    The code will throw an exception but catch block handles it correctly. -> Option C
  4. Quick Check:

    Exception thrown and caught properly [OK]
Quick Trick: Catch(Exception e) catches all exceptions [OK]
Common Mistakes:
MISTAKES
  • Thinking catch syntax is wrong
  • Assuming exception is not caught
  • Believing try block has syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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