为什么我不能在Object类引用下捕获异常,而Object类是所有类的超类?

huangapple go评论63阅读模式
英文:

Why I can't be able to catch Exceptions under Object class reference when Object class is the super class for all?

问题

根据我在Google上的研究,大多数网站都说,“Throwable”是异常的最终超类。我接受了这一点,但即使Throwable是Object类的子类,为什么我不能将异常存储在Object类下呢?

图片链接

英文:

From my research on google, most sites say that "Throwable" is the ultimate superclass for Exceptions. I accept it, but even when Throwable is a sub-class of Object class why I can't able to store exceptions under Object class?

https://i.stack.imgur.com/3EZsz.jpg

答案1

得分: 3

你不能抛出任意对象,因为语言规定你不能这样做:JLS 14.18

你不能捕获任意对象,因为语言规定你不能这样做:JLS 14.20

其意图是将异常对象限制在特定的层次结构内。

英文:

You can't throw an arbitrary object because the language says you can't: JLS 14.18

You can't catch an arbitrary object because the language says you can't: JLS 14.20

The intent is to restrict exception objects to a particular hierarchy.

huangapple
  • 本文由 发表于 2020年8月21日 22:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63524784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定