Reputation: 89593
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();
// ....
Upvotes: 3
Views: 85
Reputation: 201409
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.
Upvotes: 1