无抛出 VirtualMachineError 保证 [英] No-throw VirtualMachineError guarantees

查看:19
本文介绍了无抛出 VirtualMachineError 保证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从 C++ 到 Java 的.在 C++ 世界中,我们关注异常安全,并注意到在面对由 mutator 本身或它委托给的方法抛出的异常(minimum、strong、no-throw)时,mutator 可以提供不同的保证.实现具有强异常保证的方法需要保证一些基本操作永远不会抛出异常.JLS 声明哪些操作可以抛出哪些类型的异常,但是 VirtualMachineError 错误提出了一个问题.引用 JLS:

I've come to Java from C++. In the C++ world we pay attention to exception safety, and note that mutators can provide different guarantees in the face of exceptions thrown by the mutator itself or a method it delegates to (minimum, strong, no-throw). Implementing a method that has a strong exception guarantee requires that some basic operations are guaranteed never to throw an exception. The JLS makes statements about which operations can throw which kinds of exceptions, but the VirtualMachineError error presents a problem. Quoth the JLS:

内部错误或资源限制阻止了 Java 虚拟机器从实现Java编程的语义语;在这种情况下,一个子类的实例VirtualMachineError 被抛出.

an internal error or resource limitation prevents the Java virtual machine from implementing the semantics of the Java programming language; in this case, an instance of a subclass of VirtualMachineError is thrown.

JLS 不再提及 VirtualMachineError.内部错误"意味着 JVM 中的错误,所以我对这种情况不感兴趣:面对 JVM 中的错误,所有赌注都没有了.但是资源限制"的情况呢?有没有因为资源限制而保证永远不会失败的操作?

The JLS says no more about VirtualMachineError. An "internal error" means a bug in the JVM, so I'm not interested in that case: in the face of bugs in the JVM, all bets are off. But what about the "resource limitation" case? Are there any operations that are guaranteed never to fail because of a resource limitation?

推荐答案

引用 Java 虚拟机规范:

此规范无法预测内部错误或资源的位置可能会遇到限制,并且没有明确规定何时他们可以被举报.因此,任何 VirtualMachineError 子类下面定义的可以在运行期间的任何时间抛出Java虚拟机:

This specification cannot predict where internal errors or resource limitations may be encountered and does not mandate precisely when they can be reported. Thus, any of the VirtualMachineError subclasses defined below may be thrown at any time during the operation of the Java virtual machine:

因此在 Java 中不能对 VirtualMachineError 异常做出异常保证.所有异常保证都必须符合条件...但如果 VirtualMachineError 被抛出"则不然.这是 Java 与 C++ 不同的地方之一.

In Java therefore no exception guarantees can be made with respect to VirtualMachineError exceptions. All exception guarantees must be subject to the qualification "... but not if a VirtualMachineError is thrown". This is one of the ways in which Java is different from C++.

这也表明捕获 VirtualMachineError 异常没有多大意义,因为如果抛出异常,程序处于未定义状态.不幸的是,这包括 OutOfMemoryError 异常.不幸的是,因为如果一个程序有几个独立的任务要执行(例如,一个 Web 服务器),如果一个任务因为需要太多内存而失败,我们可能想继续执行其他任务.

This also suggests that there is not much point in catching a VirtualMachineError exception, because the program is in an undefined state if one has been thrown. That unfortunately includes OutOfMemoryError exceptions. Unfortunate, because if a program has several independent tasks to perform (for example, a web server), if one task fails because it needs too much memory, we might want to continue with the other tasks.

这篇关于无抛出 VirtualMachineError 保证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆