Defining Custom Exception in Java
Given the code fragment: You must define the A exception class. The program execution must be terminated if the condition at line 19 is true and an A exception is thrown at line 20. Which code fragment at line n1 defines A as per the requirement? - 
Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
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 fromThrowable 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?
While Throwable is the superclass, extending it directly is too broad. RuntimeException is the standard parent for unchecked exceptions that terminate programs naturally.
Does extending Exception cause termination?
No, extending Exception creates a checked exception. The compiler forces you to handle it, so it won't necessarily terminate the program if caught.