You can't catch a generic exception, because the Java Language Specification explicitly forbids it. In 14.20 The try statement it says:
It is a compile-time error if a type variable is used in the denotation of the type of an exception parameter.
The section does not explain why, but the most like reason is that <T extends Throwable>
is erased to Throwable
, so your code would actually be compiled as if it was:
public static void test(Class<? extends Throwable> t){
try{
//do work
}
catch (Throwable e){
Logger.global.info(...)
}
}
This is not the intent expressed by your code (and very likely not what you want), therefor the language specification explicitly forbids it.
Note that throwing generic exceptions is allowed, but this is usually only relevant in the context of having some wrapper around another method that throws the exception.