You cannot catch a general exception because the Java language specification explicitly forbids it. At 14.20, the try statement says:
This is a compile-time error if a type variable is used in the type designation of the exception parameter.
The section does not explain why, but the most likely reason is that <T extends Throwable> is erased to Throwable , so your code will be compiled as if it were:
public static void test(Class<? extends Throwable> t){ try{
This is not the intention expressed by your code (and most likely not what you want), so the language specification explicitly forbids it.
Note that the exception of general exceptions is allowed, but this usually only makes sense in the context of having some kind of wrapper around another method that throws an exception.
source share