Java Reference
In-Depth Information
Note that the Forward to handler code must operate correctly in constrained
memoryconditionsbecausethestackorheapmaybenearlyexhausted.Insuchascenario,
oneuseful technique isforthe program toinitially reserve memory specifically tobeused
by an out-of-memory exception handler.
Note that this solution catches Throwable in an attempt to handle the error; it falls un-
der exception ERR08-EX2 in “ERR08-J. Do not catch NullPointerException or any of
its ancestors” [Long 2012].
Applicability
Allowing a system error to abruptly terminate a Java program may result in a denial-of-
service vulnerability.
In the event of actually running out of memory, it is likely that some program data
will be in an inconsistent state. Consequently, it might be best to restart the process. If an
attempt is made to carry on, reducing the number of threads may be an effective work-
around. This measure can help in such scenarios because threads often leak memory, and
their continued existence can increase the memory footprint of the program.
The methods Thread.setUncaughtExceptionHandler() and
ThreadGroup.uncaughtException() can be used to help deal with an OutOfMemoryEr-
ror in threads.
Bibliography
[JLS 2013]
§11.2, “Compile-Time Checking of Exceptions”
[Kalinovsky 2004]
Chapter 16, “Intercepting Control Flow: Intercepting System Errors”
[Long 2012]
ERR08-J. Do not catch NullPointerException or any of its ancestors
35. Carefully design interfaces before releasing them
Interfaces are used to group all the methods that a class promises to publicly expose. The
implementing classes are obliged to provide concrete implementations for all of these
methods. Interfaces are a necessary ingredient of most public APIs; once released, flaws
can be hard to fix without breaking any code that implements the older version. The re-
percussions include the following.
Search WWH ::




Custom Search