JSR-666 Extension: Exception Types

February 13, 2008 4:08 PM

As an extension to the JSR-666 specification, the expert group would like to suggest the following enhancement to the Java Language Specification:

Problem:

When checking preconditions in Java code, it is very common to check whether some method argument is null, and throw an exception if it is. There are two equally valid exceptions that may be used in this case: IllegalArgumentException and NullPointerException. The decision to use one over the other is often the cause of holy wars, and can consume a great deal of productivity on internal mailing-lists and blogs.

Solution:

As such, we recommend the introduction of a new exception type:

throw new SchrödingerException<NullPointerException, IllegalArgumentException>("Value should not be null.");

(The umlaut is required, in accordance with previous discussion)

SchrödingerException instances will be of indeterminate type until they are first accessed in such a way as their type must be explicitly decided one way or another. At that point, the exception will "collapse" into one or other of the provided exception types.

It is impossible to determine if any exception is a SchrödingerException, as any attempt to examine or test its type will cause the exception identity to collapse to one of the supplied "stable" classes. It is also impossible to influence the final type of a SchrödingerException through external interference, including reflection, JVM instrumentation or byte-code enhancement, as any too-close examination of the exception will cause it to collapse prematurely.

(Further JSR-666 specifications: here, here, here)

Previously: Facebook for Dummies

Next: Apple TV 2.0 First Impressions