Why doesn't Java support generic Throwables?

后端 未结 5 1654
独厮守ぢ
独厮守ぢ 2020-12-14 17:02
class Bouncy extends Throwable {     
}
// Error: the generic class Bouncy may not subclass java.lang.Throwable

Why doesn\'t Java

5条回答
  •  借酒劲吻你
    2020-12-14 17:54

    Java Language Specification 8.1.2 Generic Classes and Type Parameters:

    This restriction is needed since the catch mechanism of the Java virtual machine works only with non-generic classes.

    Personally, I think it's because we can't get any benefits of generics inside a catch clause. We can't write catch (Bouncy ex) due to type erasure, but if we write catch (Bouncy ex), it would be useless to make it generic.

提交回复
热议问题