class Bouncy<T> extends Throwable {
}
// Error: the generic class Bouncy<T> may not subclass java.lang.Throwable
Why doesn’t Java support generic Throwables?
I realize that type erasure complicates certain things, but obviously Java gets by with a lot already, so why not push it one more notch and allow generic Throwables, with comprehensive compile-time check for potential problems?
I feel like the type erasure argument is rather weak. Currently, we can’t do:
void process(List<String> list) {
}
void process(List<Integer> list) {
}
Of course, we get by without it. I’m not asking that we should be able to do catch Bouncy<T1> and Bouncy<T2> in the same try block, but if we use them in disjoint contexts with strict compile-time enforceable rules (which is pretty much the way generics works right now), wouldn’t it be workable?
Java Language Specification
8.1.2 Generic Classes and Type Parameters:
Personally, I think it’s because we can’t get any benefits of generics inside a
catchclause. We can’t writecatch (Bouncy<String> ex)due to type erasure, but if we writecatch (Bouncy ex), it would be useless to make it generic.