Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Exception Handling
What will be the output of this code?
try {
  throw new ApplicationException("App error");
} catch (SystemException) {
  Console.WriteLine("System exception caught");
} catch (Exception ex) {
  Console.WriteLine(ex.Message);
}
AApp error
BSystem exception caught
CNo output
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Identify thrown exception type

    The code throws ApplicationException, which inherits from Exception but NOT from SystemException.
  2. Step 2: Match catch blocks

    The SystemException catch block does not catch ApplicationException, so the general Exception catch block runs, printing the message.
  3. Final Answer:

    App error -> Option A
  4. Quick Check:

    ApplicationException caught by Exception catch block [OK]
Quick Trick: ApplicationException is not a SystemException subclass [OK]
Common Mistakes:
MISTAKES
  • Assuming ApplicationException is SystemException
  • Expecting first catch block to run

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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