3

From JVMS chapter 6.3:

[...] any of the VirtualMachineError subclasses defined below [InternalError, OutOfMemoryError, StackOverflowError, UnknownError] may be thrown at any time during the operation of the Java Virtual Machine

How does the JVMS define the phrase "at any time during the operation of the Java Virtual Machine"?

How do current JVMs interpret that phrase?

Specifically, does it mean that the four errors, java.lang.InternalError, java.lang.OutOfMemoryError, java.lang.StackOverflowError, and java.lang.UnknownError, may be thrown between statements? :

// ....
A(); B(); C();
try {
     // nothing
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
D(); E(); F();
try {
     ; // semi-colon
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
G(); H(); I();
try {
     ; ; ;;  ;;;;; ; ; ;;; ;; ;; ;; ;; ; ;; ; ;; // ... semi-colons
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
J(); K(); L();
// ....
Seki
  • 11,135
  • 7
  • 46
  • 70
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • [these](http://stackoverflow.com/questions/27679173/which-java-errors-and-exceptions-may-not-be-thrown-by-empty-statements) are a bit too much related to be posted in the same 5 minutes in my opinion. – Maarten Bodewes Dec 28 '14 at 18:15
  • @MaartenBodewes-owlstead, Don't be tricked by the length. [They took hours](http://quoteinvestigator.com/2012/04/28/shorter-letter/). But I submitted them together because they reference each other. I couldn't link to "nothing". – Pacerier Dec 28 '14 at 18:17
  • OK, that's fair enough, but maybe leave a bit of space next time, this is likely to trigger all kinds of alarms :) – Maarten Bodewes Dec 28 '14 at 18:24
  • Java Virtual Machine is complex software. In complex software an unexpected situation can happen **at any time** due to software bugs (or hardware failure). For example see the `ShouldNotReachHere()` call in the source codes of OpenJDK (http://hg.openjdk.java.net/) spread all over the virtual machine (memory manager, byte code interpreter). Google: `site:openjdk.java.net ShouldNotReachHere` will give you a hint. What is the point of your question? Do you want to blame Java Virtual Machines for not giving a magic guarantees or do you want to solve some concrete practical problem? – xmojmr Dec 28 '14 at 18:56
  • @xmojmr, In the face of bugs, [all bets are off](http://stackoverflow.com/q/8728866/632951). This question is regarding the JVM specification and valid conforming JVMs' reactions towards resouce limitations and whatnot. – Pacerier Dec 28 '14 at 19:02

1 Answers1

1

The Java Virtual Machine consists of many parts. For example, the garbage collector runs as a persistent background Thread. It might throw one of those Exception and it might certainly appear to occur at any time (especially if your own code is stopped due to gc)!

From Java Garbage Collection Basics

What is Automatic Garbage Collection?

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.

tl;dr

Yes. They could be thrown between statements.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Hmm, are GC errors [not confined within the GC threads](http://stackoverflow.com/questions/27679173/which-java-errors-and-exceptions-may-not-be-thrown-by-empty-statements/27679270#comment-43773247)? – Pacerier Dec 28 '14 at 18:49
  • **Could** be `OutOfMemoryError` and your next instruction might be to allocate memory; the instructions might be re-ordered. Boom, you get GC error in both threads. – Elliott Frisch Dec 28 '14 at 19:21