The Philosophy of Nothing
Reinier Zwitserloot
reinier at zwitserloot.com
Sat Dec 5 04:46:28 PST 2009
Nope, the JLS specifically states that an exception is unchecked if it is
assignment compatible to RuntimeException or Error, and checked if it isn't.
Therefore, an exception type that is a subtype of both IOException and
RuntimeException is unambiguously unchecked.
One could claim then that type casting causes weirdness to ensue, such as:
Nothing x = getInstanceOfNothing();
throw (IOException)x;
or even worse;
IOException x = getInstanceOfNothing();
throw x;
But this isn't very weird if you think about it a little more. Consider:
RuntimeException x = new RuntimeException();
Exception y = x;
throw y;
unchecked exceptions can parade as checked exceptions with a simple silent
upcast already. Nothing does not change the equation.
checked / unchecked is a property of types, not of objects. It all works out
nicely; Nothing, being a subtype of RuntimeException (and Error), is an
unchecked throwable type. Therefore, something like:
Nothing n = getNothing();
throw n;
does not require you to catch or throw onwards the 'Nothing' type, and does
compile, as instances of Nothing are Throwables. Of course, the 'throw n;'
statement won't ever be executed; getNothing() must necessarily loop
forever, or throw an exception itself as it can't return.
There is nevertheless some weirdness inherent in having a nothing type. For
example:
Class<?> c = someType();
if (String.class.isAssignableFrom(c) && Number.class.IsAssignableFrom(c)) {
System.out.println("Hello, World!");
}
one could argue that a sufficiently advanced style checker should flag this
code as silly; no type is going to be both a subtype of Number and of
String. It's analogous to the somewhat easier to realize as being silly
code:
if (x instanceof Number && x instanceof String) { ... }
except of course that in the isAssignableFrom case, Nothing.class as a value
for c will in fact make the boolean work. I guess we're somewhat lucky that
no style checkers that I'm aware of actually go that far.
--Reinier Zwitserloot
On Sat, Dec 5, 2009 at 11:46 AM, Peter Levart <peter.levart at gmail.com>wrote:
> On Wednesday 02 December 2009 17:45:19 Reinier Zwitserloot wrote:
> > The 'throws' clause of any method declaration lists *CHECKED* exceptions
> > that you can't throw. Thus, "throws Nothing" indicates that the method
> may
> > throw any unchecked exception, like all methods can, and may also throw
> any
> > instance of Nothing.
> >
> > That last bit is trivially true for two reasons:
> >
> > 1. There are no instances of nothing. Therefore, granting any method the
> > ability to throw instances of Nothing is irrelevant.
> >
> > 2. Nothing is an unchecked exception type, as it is a subtype of either
> > RuntimeException or Error (it is in fact, both - Nothing is a subtype of
> > everything). This is consistent with existing spec, see JLS 11.2:
> >
> http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html#
> > 44121
> >
>
> and
>
> 3. Nothing is also a checked exception type, as it is as subtype of say
> IOException, which is a
> checked exception (unless the definition of checked exceptions in new JLS
> excludes Nothing from
> checked exceptions types). :-)
>
> Peter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20091205/66de70a9/attachment.html
More information about the closures-dev
mailing list