Special non-exceptions.

Overview

There are a couple of Throwables which have surprising properties.

Throwable can be extended

When extending Throwable it is checked by the compiler, through is not an Exception.
public static void main(String... args) throws MyThrowable {
    throw new MyThrowable(); // will not compile unless MyThrowable is handled.
}

class MyThrowable extends Throwable {
}
This prints out the MyThrowable with a stack trace as expected.

The slient Error

A throwable which extends ThreadDeath is silent as it is deadly. ;)
public static void main(String... args) {
    throw new MyThrowable();
}

class MyThrowable extends ThreadDeath {
}
When this runs, no error/exception is printed, the thread just exits.

This is because the error/exception is printed by ThreadGroup.uncaughtException() which ignores instanceof ThreadDeath by default.

The ThreadDeath error is used by Thread.stop() and although this method is deprecated, the ThreadDeath error is not deprecated and even describes itself as a "normal occurrence" ;)

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Unusual Java: StackTrace Extends Throwable