为什么 InstantiationException 是受检查异常?

发布于 2024-11-15 23:16:33 字数 300 浏览 2 评论 0原文

我的理解是,受检查的异常是那些可以合理预期调用者可以从中恢复的异常。我不明白为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黑凤梨 2024-11-22 23:16:33

我能想到的显式处理此异常的一个原因(但这不是权威答案):

尝试使用反射实例化一个类(因为该类是配置的,而不是静态链接的)。如果它没有预期的构造函数签名,请尝试另一个构造函数。或者其他班级。任何框架代码(例如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.

机场等船 2024-11-22 23:16:33

来自 InstantiationException 的 JavaDoc:

当应用程序尝试执行以下操作时抛出
使用创建类的实例
Class 类中的 newInstance 方法,
但指定的类对象不能
被实例化,因为它是一个
接口或者是抽象类

仅当使用 Java 反射时才会发生这种情况,例如,当以编程方式实例化对象时,例如 ClassName.class.newInstance(),而不是 new ClassName() 可以这么说。很自然地期望使用反射的人编写处理任何此类异常的代码,例如实例化抽象类或接口,或者在构造函数调用期间抛出异常(在这种情况下,您可以使用 e.getCause ())。

它不会在您的代码中处理,而是由使用反射的特定 API/库处理。

From the JavaDoc for InstantiationException:

Thrown when an application tries to
create an instance of a class using
the newInstance method in class Class,
but the specified class object cannot
be instantiated because it is an
interface or is an abstract class
.

This will only happen when using Java reflection, e.g. when programmatically instantiating objects, e.g. ClassName.class.newInstance() as opposed to new 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 use e.getCause()).

It is not expected to be handled in your code -- but rather by that particular API/library that uses reflection.

终遇你 2024-11-22 23:16:33

Class.newInstance() 对于何时引发 InstanciationException 有一个有趣的描述 [javadoc]

InstantiationException - 如果此类表示抽象类、接口、数组类、原始类型或 void;或者如果该类没有无效构造函数;或如果实例化因其他原因失败

对我来说,它似乎试图涵盖静态链接类的实例化在编译时失败的所有情况。

最重要的部分是我强调的部分。 想象一个抛出检查异常的构造函数。如果动态调用该构造函数会发生什么?谁来检查那个可怜的已检查异常?

Class.newInstance() has an interesting description on when an InstanciationException is thrown [javadoc] :

InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.

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?

花桑 2024-11-22 23:16:33

正如您可以从 InstantiationException javadoc 的 javadoc 中看到的,它被抛出

当应用程序尝试创建
使用类的实例
类 Class 中的 newInstance 方法,但是
指定的类对象不能是
实例化。

你可以完美地编写这样的代码:

try {
Class myClass = Class.forName("Myclass");
myClass.newInstance();
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}

不会抛出 IllegalArgumentException

关于checkedunchecked更多的是关于导致异常的原因,而不是是否容易恢复。请详细了解 checked

As you can see from javadoc of InstantiationException javadoc, it's thrown

when an application tries to create an
instance of a class using the
newInstance method in class Class, but
the specified class object cannot be
instantiated.

you can perfectly write such code:

try {
Class myClass = Class.forName("Myclass");
myClass.newInstance();
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}

no IllegalArgumentException will be thrown.

About checked and unchecked it's more about what caused the exception, not whether it's easy to recover from or not. Please read more about checked vs

酷到爆炸 2024-11-22 23:16:33

虽然检查异常和非检查异常之间存在巨大的灰色区域,并且许多异常可以按一种或另一种方式设计,但这个不是。这是一个错误,应该不予检查。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文