0

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?

4
  • Do you know about checked and unchecked exceptions? What kind is IllegalArgumentException? Commented Nov 20, 2016 at 2:34
  • @SotiriosDelimanolis I don't know Commented Nov 20, 2016 at 2:37
  • @Ogen then google it, check the docs: docs.oracle.com/javase/7/docs/api/java/lang/… and see that it extends a java.lang.RuntimeException Commented Nov 20, 2016 at 2:39
  • 1
    Reminds me of this: stackoverflow.com/questions/31316581/… Commented Nov 20, 2016 at 2:54

1 Answer 1

2

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.

This gets easy to see once you try to understand what happens with the generic method:

methodWithExceptionGeneric(new IllegalArgumentException());

is equivalent to

methodWithExceptionGeneric<IllegalArgumentException>(new IllegalArgumentException());

When we take a look at the definition

private static <T extends Throwable> void methodWithExceptionGeneric(final T t) throws T ...

turns into

private static <IllegalArgumentException> void methodWithExceptionGeneric(IllegalArgumentException) throws IllegalArgumentException ...

So methodWithExceptionGeneric(new IllegalArgumentException()); can only throw a IllegalArgumentException or any other unchecked Exception per definition. The non-generic method on the other hand can throw any Exception, be it checked or unchecked and thus must be called from within try-catch-block handling anything the method throws.

Sign up to request clarification or add additional context in comments.

2 Comments

So how would I need to change the non generic method to make the compile time error go away? I changed the parameter type to RuntimeException so that it's an unchecked exception and supposedly I don't have to catch it but the compiler is still complaining.
@Ogen It's complaining because within the method you're throwing t which is a Throwable, which is a checked exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.