为什么 InstantiationException 是受检查异常?
我的理解是,受检查的异常是那些可以合理预期调用者可以从中恢复的异常。我不明白为什么 InstantiationException 会出现这种情况。如果一个类无法实例化,那么调用者应该做什么?
然后我想,也许代码编译是一个重要的考虑因素 - 因此只有在动态指定类的情况下才会发生这种情况。1 在这种情况下,类可能更像是一个参数,但随后我们有 IllegalArgumentException,它是一个运行时异常。
检查标准异常和不检查标准异常背后的原理是什么?
1 这是真的吗?
My understanding is that checked exceptions are those that the caller of the can reasonably be expected to recover from. I don't understand why this is the case with InstantiationException. If a class cannot be instantiated then what is the caller expected to do?
I then thought that maybe it was an important consideration that the code had compiled - therefore this could only happen if a class is dynamically specified.1 In this case the class may be more like a parameter, but then we have IllegalArgumentException that is a runtime exception.
What is the rational behind which standard exceptions are checked, and which are not?
1 Is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能想到的显式处理此异常的一个原因(但这不是权威答案):
尝试使用反射实例化一个类(因为该类是配置的,而不是静态链接的)。如果它没有预期的构造函数签名,请尝试另一个构造函数。或者其他班级。任何框架代码(例如Spring)都可能有这样的逻辑。
One reason for explicitly handling this exception that I can think of (but that's not an authoritative answer):
Try instanciating a class with reflection (because that class is configured, not statically linked). If it doesn't have the expected constructor signature, try another constructor. Or another class. Any framework code (such as Spring) might have such logic.
来自 InstantiationException 的 JavaDoc:
仅当使用 Java 反射时才会发生这种情况,例如,当以编程方式实例化对象时,例如
ClassName.class.newInstance()
,而不是new ClassName()
可以这么说。很自然地期望使用反射的人编写处理任何此类异常的代码,例如实例化抽象类或接口,或者在构造函数调用期间抛出异常(在这种情况下,您可以使用e.getCause ()
)。它不会在您的代码中处理,而是由使用反射的特定 API/库处理。
From the JavaDoc for InstantiationException:
This will only happen when using Java reflection, e.g. when programmatically instantiating objects, e.g.
ClassName.class.newInstance()
as opposed tonew ClassName()
so to speak. It is only natural to expect whoever uses reflection to write code that handles any such aberrations like instantiating an abstract class or an interface or if there is an exception thrown in during the constructor invocation (in which case you can usee.getCause()
).It is not expected to be handled in your code -- but rather by that particular API/library that uses reflection.
Class.newInstance() 对于何时引发 InstanciationException 有一个有趣的描述 [javadoc]:
对我来说,它似乎试图涵盖静态链接类的实例化在编译时失败的所有情况。
最重要的部分是我强调的部分。 想象一个抛出检查异常的构造函数。如果动态调用该构造函数会发生什么?谁来检查那个可怜的已检查异常?
Class.newInstance() has an interesting description on when an InstanciationException is thrown [javadoc] :
To me it looks like it tries to cover for all cases a statically linked class' instantiation would fail at compile time.
The most important part is the piece I highlighted though. Imagine a constructor that throws a checked exception. What happens if that constructor is called dynamically? Who'll check for that poor checked exception?
正如您可以从 InstantiationException javadoc 的 javadoc 中看到的,它被抛出
你可以完美地编写这样的代码:
不会抛出
IllegalArgumentException
。关于
checked
和unchecked
更多的是关于导致异常的原因,而不是是否容易恢复。请详细了解checked
与As you can see from javadoc of InstantiationException javadoc, it's thrown
you can perfectly write such code:
no
IllegalArgumentException
will be thrown.About
checked
andunchecked
it's more about what caused the exception, not whether it's easy to recover from or not. Please read more aboutchecked
vs虽然检查异常和非检查异常之间存在巨大的灰色区域,并且许多异常可以按一种或另一种方式设计,但这个不是。这是一个错误,应该不予检查。
While there's a vast grey area between check and unchecked exceptions, and many exceptions can be arguably designed one way or the other, this one is not. It is a mistake, it should have been unchecked.