According to the Java Language Sepecification, 3rd edition:
It is a compile-time error if a generic class is a direct or indirect subclass of
Throwable
.
I wish to understand why this decision has been made. What's wrong with generic exceptions?
(As far as I know, generics are simply compile-time syntactic sugar, and they will be translated to Object
anyway in the .class
files, so effectively declaring a generic class is as if everything in it was an Object
. Please correct me if I'm wrong.)
Here is a simple example of how to use the exception:
The body of the TRy statement throws the exception with a given value, which is caught by the catch clause.
In contrast, the following definition of a new exception is prohibited, because it creates a parameterized type:
An attempt to compile the above reports an error:
This restriction is sensible because almost any attempt to catch such an exception must fail, because the type is not reifiable. One might expect a typical use of the exception to be something like the following:
This is not permitted, because the type in the catch clause is not reifiable. At the time of this writing, the Sun compiler reports a cascade of syntax errors in such a case:
Because exceptions cannot be parametric, the syntax is restricted so that the type must be written as an identifier, with no following parameter.
I would expect that it's because there's no way to guarantee the parameterization. Consider the following code:
As you note, parameterization is just syntactic sugar. However, the compiler tries to ensure that parameterization remains consistent across all references to an object in compilation scope. In the case of an exception, the compiler has no way to guarantee that MyException is only thrown from a scope that it is processing.
As mark said, the types are not reifiable, which is a problem in the following case:
Both
SomeException<Integer>
andSomeException<String>
are erased to the same type, there is no way for the JVM to distinguish the exception instances, and therefore no way to tell whichcatch
block should be executed.It's essentially because it was designed in a bad way.
This issue prevents clean abstract design e.g.,
The fact that a catch clause would fail for generics are not reified is no excuse for that. The compiler could simply disallow concrete generic types that extend Throwable or disallow generics inside catch clauses.
Generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called type erasure. For example,
List<Integer>
will be converted to the non-generic typeList
.Because of type erasure, type parameters cannot be determined at run-time.
Let's assume you are allowed to extend
Throwable
like this:Now let's consider the following code:
Due to type erasure, the runtime will not know which catch block to execute.
Therefore it is a compile-time error if a generic class is a direct or indirect subclass of Throwable.
Source: Problems with type erasure