Bird
0
0

Given the following code, what will be the output?

hard📝 Application Q8 of 15
Java - Exception Handling

Given the following code, what will be the output?

public class Test {
    public static void main(String[] args) {
        try {
            throw new Exception("Test Exception");
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
            throw new RuntimeException("Runtime after catch");
        }
    }
}
ACompilation error due to throw in catch
BCaught: Test Exception only
CProgram compiles but no output
DCaught: Test Exception followed by program termination with RuntimeException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try-catch behavior

    The try block throws Exception, caught and message printed.
  2. Step 2: Analyze throw in catch block

    The catch block throws a new RuntimeException, which is unchecked and not caught.
  3. Step 3: Understand program termination

    Program prints the message then terminates due to uncaught RuntimeException.
  4. Final Answer:

    Caught: Test Exception followed by program termination with RuntimeException -> Option D
  5. Quick Check:

    Throw in catch rethrows exception, program terminates [OK]
Quick Trick: Throwing inside catch rethrows exception, program ends if uncaught [OK]
Common Mistakes:
  • Expecting only message output
  • Thinking throw in catch causes compile error
  • Assuming program continues after throw
  • Ignoring RuntimeException behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes