Defining Custom Exception in Java
Short answer
D Define class A by extending RuntimeException so that the exception is unchecked and causes immediate program termination if not caught.
Question

Key Takeaways
The question asks how to ensure the program terminates when an exception is thrown, which requires defining an unchecked exception (RuntimeException) rather than a checked one.
This question tests the ability to define a custom exception class that terminates program execution immediately upon being thrown. It establishes that extending RuntimeException is the standard approach for unchecked exceptions that bypass mandatory catch blocks.
Common mistake: Many learners choose 'class A extends Throwable' or 'class A extends Exception' because they believe these are the base classes for all exceptions. However, extending Exception creates a checked exception, which would require explicit handling and might not terminate the program as expected if caught elsewhere.
Expert Analysis
Why the Answer Is Correct
The correct answer is D: class A extends RuntimeException. In Java, exceptions derived from RuntimeException are unchecked exceptions. This means the compiler does not force the programmer to handle them using try-catch blocks. When such an exception is thrown at line 20, it propagates up the call stack until it is either caught or reaches the main method, causing the program to terminate abruptly. This behavior aligns perfectly with the requirement that "the program execution must be terminated".
Why the Other Options Are Wrong
Option A (extends Throwable) is technically possible but generally discouraged for application-level exceptions; Throwable is the root of both Errors and Exceptions. Option B (extends Exception) creates a checked exception, which requires explicit handling and does not guarantee termination if caught. Option C (extends ArithmeticException) is incorrect because ArithmeticException is already a specific runtime exception for arithmetic errors; creating a custom exception by extending a specific subclass restricts its semantic meaning and is less flexible than extending RuntimeException directly.
Community Comment Notes
The community discussion highlights confusion between checked and unchecked exceptions. One commenter noted that exceptions extended from Throwable but not Exception (like Error) are not caught, implying a misunderstanding of the hierarchy. Another comment correctly identifies that RuntimeException ensures the program finishes without needing explicit catch clauses, reinforcing why D is the best choice over A or B.
Exam Strategy
When asked about terminating a program via exception, look for unchecked exceptions like RuntimeException. Remember that checked exceptions (Exception subclasses) must be handled, while unchecked ones propagate automatically, often leading to program termination if uncaught.
Frequently Asked Questions
Why isn't extending Throwable the correct answer?
Does extending Exception cause termination?
Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
ASPushkin 👍 1answer : A B is not correct According to the source code only exceptions extended from Throwable type and not from an Exception type are not caught. It could be Error or even checked type of exception.
-
d7bb0b2 👍 1 Answered Aa IS CORRECT for finish program in line 20, others catch in exception clause an conitnue program
-
ASPushkin 👍 1 Answered Asee below
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.