Ogen Ogen - 13 days ago 5
Java Question

Java generics with exceptions producing compile time errors

I have these two methods:

private static <T extends Throwable> void methodWithExceptionGeneric(final T t) throws T {
throw t;
}

private static void methodWithExceptionNonGeneric(final Throwable t) throws Throwable {
throw t;
}


When I call these methods like so:

methodWithExceptionGeneric(new IllegalArgumentException());
methodWithExceptionNonGeneric(new IllegalArgumentException()); // compile time error


I get a compile time error in the non generic method saying that I have an unhandled exception in my main method and I need to either declare a throws statement or catch the exception.

My question is: why is it only complaining about the non generic method? In my mind, the generic method is also throwing an exception so shouldn't that have to be handled too?

Answer

The reason is pretty simple:
IllegalArgumentException is a RuntimeException, which means it's an unchecked exception. You can catch it, but you don't have to. Since the generic method only throws IllegalArgumentException by it's specification, the compiler won't complain (unchecked exception).

The method without generics on the other hand is specified to throw any Throwable, which means it can also throw unchecked exceptions (and errors), which need to be handled.

Comments