Defining Custom Exception in Java

Exception handling
Answer Correct answer: D — Define class A by extending RuntimeException so that the exception is unchecked and causes immediate program termination if not caught.

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? - image

  1. class A extends Throwable {
  2. class A extends Exception {
  3. class A extends ArithmeticException {
  4. class A extends RuntimeException { Correct Answer

Community Votes

A
100%

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)

d7bb0b2 👍 1 Selected: A
a IS CORRECT for finish program in line 20, others catch in exception clause an conitnue program
ASPushkin 👍 1 Selected: A
see below
ASPushkin 👍 1
answer : 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.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

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.

More 1Z0-819 FAQ →

Related Analysis

← Back to 1Z0-819 Study Guide