Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Why cannot type parameters be in a catch clause?

I know that generic types cannot extends Throwable and I understand that this doesn't make sense because type erasure (exceptions are thrown only at runtime) and other subtle things.

However Java allows a type parameter to be bounded by Throwable in fact the following code is legal

class MyClass<T extends Throwable> { /*body of the class */ }

What is illegal is the use of the T parameter in a catch clause

try { //do something that throws exception //  }
catch(T e) /* ILLEGAL!!! DOESN'T COMPILE */ {}

The only reason that I can understand about this constraint is that the type parameter, because erasure, is replaced by its bounding type, in this case Throwable. But type parameters could be bounded also by more than one bounding type so I could have this situation

class MyClass<T extends Object & Throwable> [*]

in such case T is replaced after erasure by Object and I know that an Object (that is not of type Throwable) variable cannot be inside a catch clause.

Let me know if this is the reason about this Java constraint. Thanks.

EDIT: [*] As davidxxx make me notice, this class declaration doesn't compile because Throwable is not an inteface.

like image 347
Vin Avatar asked Jul 24 '17 14:07

Vin


People also ask

Can we write methods in catch block?

You must Call a method in catch block iff you want that method to perform a specific task when an Exception is caught /thrown . Example : you may want to print the reason for an exception occured ;) Show activity on this post.

Which data type Cannot be type parameterized?

Which of these data type cannot be type parameterized? Explanation: None. Sanfoundry Certification Contest of the Month is Live.

Which can handle all type of exception in catch clause?

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception. Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final .

Can catch block have multiple parameters?

Explanation: A catch handler should contain only one parameter of any type. Multiple parameters are not allowed inside catch handler.


1 Answers

There is no way for the runtime to check if the type of a caught exception matches T because it does not know what T is.

See this question for some additional information - type parameters are kept on methods and classes, but not variables.

The point of a catch block is that you can handle exceptions by providing specific behavior to the exception type. This is also why you can supply multiple catch blocks for a single try. Without the specific exception type it would not be possible to execute the correct catch block.

Also, what do you expect the code to do if you run it with a raw type? The way you propose, T would be replaced with Throwable and the catch block would now catch all exceptions instead of only those of a certain type.

Additionally, the JLS specifies CatchType as:

CatchType:
    ClassType
    ClassType | CatchType

which does not include TypeArgument.

If you really want to do this, you can catch Throwable and check the type using instanceof or using its Class:

try {
    doSomethingDangerous();
} catch (Throwable t) {
    if (t instanceof IOException) {
        handleIoException(t);
    } else if (...) {
        ...
    } else {
        handleOther(t);
    }
}

Note: Catching Throwable or Exception is usually considered very bad practice.

Edit: here's what it looks like in C# for possible reference.

like image 60
Salem Avatar answered Oct 20 '22 02:10

Salem