Bird
0
0

What will be the output of the following C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Exception Handling
What will be the output of the following C# code?
try {
  throw new Exception("Error 1");
} catch (Exception ex) {
  Console.WriteLine(ex.Message);
  throw;
}
AError 1 printed, then program terminates with the same exception.
BNo output, program silently terminates.
CError 1 printed, then program continues normally.
DCompilation error due to missing catch block.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    The try block throws an exception with message "Error 1".
  2. Step 2: Analyze the catch block

    The catch block prints the exception message, then rethrows the same exception using throw;.
  3. Step 3: Understand program flow after rethrow

    Since the exception is rethrown and not caught again, the program terminates with the same exception after printing.
  4. Final Answer:

    Error 1 printed, then program terminates with the same exception. -> Option A
  5. Quick Check:

    Print then rethrow = B [OK]
Quick Trick: Rethrow after print causes termination with printed message [OK]
Common Mistakes:
MISTAKES
  • Assuming program continues after rethrow
  • Thinking no output is printed
  • Confusing rethrow with new exception creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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