Java "throw" targets: which of the following types can legally be thrown with the throw statement? Consider: 1) Error, 2) Event, 3) Object, 4) Throwable, 5) Exception, 6) RuntimeException.

Difficulty: Easy

Correct Answer: 1, 4, 5 and 6

Explanation:

Introduction / Context:This question checks your knowledge of Java’s Throwable hierarchy and the compile-time rules governing the throw statement. Only objects whose types extend Throwable may be thrown.

Given Data / Assumptions:

  • Candidate types: Error, Event, Object, Throwable, Exception, RuntimeException.
  • We must select all and only the types permitted by the Java Language Specification for throw.

Concept / Approach:The throw statement requires an expression of type Throwable or a subtype. The hierarchy is: Throwable → {Error, Exception}. RuntimeException extends Exception. Neither Event nor a bare Object derives from Throwable, so they cannot be thrown.

Step-by-Step Solution:Check 1) Error: subclass of Throwable ⇒ allowed.2) Event: not a Throwable ⇒ not allowed.3) Object: root of all objects but not a Throwable ⇒ not allowed.4) Throwable: allowed.5) Exception: allowed.6) RuntimeException: allowed.Hence, the correct set is 1, 4, 5, and 6.

Verification / Alternative check:Try throw new Object(); in a test program; it fails to compile. Replace with throw new Throwable(); or throw new Error(); and it compiles (with appropriate handling/declaring for checked types).

Why Other Options Are Wrong:Options A/B/D include non-Throwable types (Event or Object) and are therefore invalid.

Common Pitfalls:Confusing “any object” with Throwable-only; forgetting that RuntimeException is a subclass of Exception and both are Throwable.

Final Answer:1, 4, 5 and 6

More Questions from Exceptions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion