Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Exception Handling
Identify the error in this C# code snippet:
try { int x = 10; int y = 0; int z = x / y; } catch { Console.WriteLine("Error caught"); }
AConsole.WriteLine is not allowed in catch
BDivision by zero is allowed
CTry block syntax is incorrect
DMissing exception type in catch block
Step-by-Step Solution
Solution:
  1. Step 1: Check catch block syntax

    In C#, catch blocks should specify exception type or use empty parentheses.
  2. Step 2: Identify missing parentheses

    The catch block here misses parentheses, causing syntax error.
  3. Final Answer:

    Missing exception type in catch block -> Option D
  4. Quick Check:

    Catch block syntax error = A [OK]
Quick Trick: Catch block needs parentheses even if no type specified [OK]
Common Mistakes:
MISTAKES
  • Assuming division by zero is allowed
  • Ignoring missing parentheses
  • Thinking Console.WriteLine is invalid in catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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