不抛出 VirtualMachineError 保证
我是从 C++ 转向 Java 的。在 C++ 世界中,我们关注异常安全,并注意到,面对变元本身或其委托的方法抛出的异常,变元可以提供不同的保证(最小、强、不抛出)。实现具有强大异常保证的方法需要保证一些基本操作永远不会抛出异常。 JLS 声明了哪些操作可以抛出哪些类型的异常,但是 VirtualMachineError 错误提出了一个问题。引用 JLS:
内部错误或资源限制阻止 Java 虚拟 机器从实现Java编程的语义 语言;在这种情况下,子类的实例 抛出
VirtualMachineError
。
JLS 不再提及 VirtualMachineError
。 “内部错误”意味着 JVM 中的错误,所以我对这种情况不感兴趣:面对 JVM 中的错误,所有的赌注都失败了。但“资源限制”的情况又如何呢?是否有任何操作可以保证永远不会因为资源限制而失败?
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:
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 ofVirtualMachineError
is thrown.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引用 Java 虚拟机规范:
因此,在 Java 中,不能对
VirtualMachineError
异常做出任何异常保证。所有异常保证都必须遵守“...但如果抛出VirtualMachineError
则不然”的条件。这是Java 与C++ 的不同之处之一。这也表明捕获 VirtualMachineError 异常没有多大意义,因为如果抛出异常,程序将处于未定义状态。不幸的是,这包括
OutOfMemoryError
异常。不幸的是,因为如果一个程序有几个独立的任务要执行(例如,一个 Web 服务器),如果一个任务因为需要太多内存而失败,我们可能想继续执行其他任务。Quoth the Java Virtual Machine Specification:
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 aVirtualMachineError
is thrown". This is one of the ways in which Java is different from C++.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 includesOutOfMemoryError
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 的完美示例的链接。 虚拟机错误
此错误与 OutofMemoryError 不同,此时某些操作可能正在进行中。
If it is resource limitation, on the first place, no operations takes place. Here is link for perfect example to have VirtualMachineError. Virtual machine error
This error is not something like OutofMemoryError, where by that time some operations might be in progress.
我看到您已经回答了自己的问题,并且我可以理解为什么来自严格的 C++ 背景的您会感到有点惊讶。这就是托管内存(虚拟)机的现实,并且不仅仅限于 java。内存可能会耗尽,因为 JVM 受限于它可以使用的堆大小(可在 java 命令行上配置)。
有点类似,但不等同,在 C++/机器代码世界中,当尝试寻址尚未分配或位于虚拟地址之外的内存时,会出现 GENERAL_PROTECTION_FAULT(如果使用 *NIX,则为 SEGMENTATION_FAULT)。空间。面对这种情况提供“强大的异常保证”同样困难,因为原因可能是代码中的错误或完全超出程序的控制范围。
I see that you've answered your own question and I can understand why this would be mildly surprising to you, coming from a strict C++ background. This is just the reality of managed memory (virtual) machines and it's not limited to just java. Memory can run out, as the JVM is bounded to how much heap it can use (configurable on the java command line).
Somewhat analogous, but not equivalent, in the C++/machine-code world would be a GENERAL_PROTECTION_FAULT (or SEGMENTATION_FAULT if you're on *NIX) that you would get when attempting to address memory that has not been allocated or is outside your virtual address space. Providing a "strong exception guarantee" in the face of that scenario is equally difficult as the cause may be either a bug in code or completely outside the control of the program.
在Java中,您可以随时调用Thread.stop()或stop(Throwable)。大多数错误都被认为非常严重,除非您真正知道自己在做什么,否则不应尝试处理它们。
我开发服务器端 Java 应用程序已有 12 年了,可以说我从未听说过有人担心抛出随机异常。我怀疑这不是 Java 需要担心的问题。
您能否举例说明为什么您认为自己需要保证,因为可能有另一种方法来解决问题?
In Java, you can call Thread.stop() or stop(Throwable) at any time. Most errors are considered so critical that you should not try to handle them unless you really know what you are doing.
Having developed server side Java application for 12 years, I can say I have never heard of anyone worrying about random exceptions being thrown. I suspect its just not an issue you need to worry about with Java.
Can you give an example of why you believe you need a guarantee, as there is likely to be another way to solve the problem?