Why doesn't Java support generic Throwables?

后端 未结 5 1656
独厮守ぢ
独厮守ぢ 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

    Short answer: because they took shortcuts, just like they did with erasure.

    Long answer: as others already indicated, because of erasure, there is no way to make a difference at runtime between a "catch MyException" and "catch MyException".

    But that doesn't mean that there is no need for generic exceptions. I want generics to be able to use generic fields! They could have simply allowed generic exceptions, but only allow catching them in the raw state (e.g. "catch MyException").

    Granted, this would make generics even more complicated. This is to show just how bad the decision to erase generics was. When will we have a Java version that supports real generics (with RTTI), not the current syntactic sugar?

提交回复
热议问题