Java Reference
In-Depth Information
Noncompliant Code Example
This noncompliant code example generates a StackOverflowError as a result of infinite
recursion. It exhausts the available stack space and may result in denial of service.
Click here to view code image
public class StackOverflow {
public static void main(String[] args) {
infiniteRun();
// ...
}
private static void infiniteRun() {
infiniteRun();
}
}
Compliant Solution
This compliant solution shows a try-catch block that can be used to capture
java.lang.Error or java.lang.Throwable . A log entry can be made at this point, fol-
lowed by attempts to free key system resources in the finally block.
Click here to view code image
public class StackOverflow {
public static void main(String[] args) {
try {
infiniteRun();
} catch (Throwable t) {
// Forward to handler
} finally {
// Free cache, release resources
}
// ...
}
private static void infiniteRun() {
infiniteRun();
}
}
Search WWH ::




Custom Search