不抛出 VirtualMachineError 保证

No-throw VirtualMachineError guarantees(不抛出 VirtualMachineError 保证)

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

问题描述

我是从 C++ 来到 Java 的.在 C++ 世界中,我们关注异常安全性,并注意到,面对由 mutator 本身或它委托给的方法抛出的异常(最小、强、无抛出),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 保证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:不抛出 VirtualMachineError 保证

基础教程推荐