Compile time error: The generic class may not subclass java.lang.Throwable
public class TestGenericClass<E> extends Exception {
/*Above line will give compile error, the generic class TestGenericClass<E> may
not subclass java.lang.Throwable*/
public TestGenericClass(String msg) {
super(msg);
}
}
Above compile time error is for the reason given in § jls-8.1.2 as below, and explained in this question:
It is a compile-time error if a generic class is a direct or indirect subclass of Throwable(§11.1.1).
This restriction is needed since the catch mechanism of the Java Virtual Machine works only with non-generic classes.
Question:
How it is restricted that subclass of
java.lang.Throwable
will not be generic class?Or more generic question will be, how to restrict that subclasses of any class cannot be generic?
Generic
class. – CaptainThrowable
, if you must let the compiler complain when someone tries to do that. – AryanizeThrowable
when we are creating someException
orError
class. This will give problem if I will have to extend class likeApplet
– Captainjava.lang.Throwable
– Captain