Search code examples
javaoperating-systemjvmvirtual-machinejls

What operations may (not) throw StackOverflowError?


When will a StackOverError be thrown?

Or rather, when will it not be thrown?

For example, if we use the primitive operators +, +=, -, -=, == <, >, /, %, etc:

try {
     // operations +, +=, -, -=, == <, >, /, %, etc
} catch (java.lang.StackOverflowError e) {
     // will never occur?
}

Is there any guarantee that StackOverflowError will not be thrown?


Solution

  • is it true that code which do not call any functions will never throw a java.lang.StackOverflowError?

    A StackOverflowError is-a VirtualMachineError. It transpires that there are no no-throw guarantees about VirtualMachineErrors. The Java Virtual Machine Specification says the following (emphasis added).

    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: ... StackOverflowError