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.
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 of these data type cannot be type parameterized? Explanation: None. Sanfoundry Certification Contest of the Month is Live.
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 .
Explanation: A catch handler should contain only one parameter of any type. Multiple parameters are not allowed inside catch handler.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With