Bird
Raised Fist0

In this code, what will be printed?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Exception Handling
In this code, what will be printed?
static int Test() {
    try {
        throw new Exception("Error");
    } finally {
        return 10;
    }
}

Console.WriteLine(Test());
AThrows exception and prints nothing.
BPrints 10 because finally return overrides exception.
CPrints 0 because exception is ignored.
DCompilation error due to return in finally.
Step-by-Step Solution
Solution:
  1. Step 1: Exception thrown in try block

    Exception is thrown but not caught.
  2. Step 2: finally block has return statement

    Return in finally overrides the exception, so method returns 10.
  3. Final Answer:

    Prints 10 because finally return overrides exception. -> Option B
  4. Quick Check:

    finally return overrides exceptions = B [OK]
Quick Trick: Return in finally overrides exceptions [OK]
Common Mistakes:
MISTAKES
  • Expecting exception to propagate
  • Thinking return in finally causes error
  • Ignoring finally block effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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