Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: revamped exceptions

Few, if any, methods are capable of handling all possible runtime exceptions. Consequently, methods are forbidden to catch RuntimeException. When a method catches RuntimeException, it may receive exceptions unanticipated by the designer, such as NullPointerException. Many catch clauses simply log or ignore the enclosed exceptional condition, and attempt to resume normal execution; this practice often violates guideline EXC00-J. Do not suppress or ignore checked exceptions. Runtime exceptions often indicate bugs in the program that should be fixed by the developer, and often cause control flow vulnerabilities. Methods are also forbidden to catch from catching Exception or Throwable, because this implies catching RuntimeException; RuntimeException extends Exception which in turn extends Throwable.

...

Code Block
bgColor#FFcccc
try {
  division(200,5);
  division(200,0); // Divide by zero        
} catch (ArithmeticException ae) { 
  throw new DivideByZeroException(); 
} catch (Exception e) { 
  System.out.println("Exception occurred :" + e.getMessage());
}	

Note that DivideByZeroException is a custom exception type that extends Exception.

Compliant Solution

This compliant solution catches only the specific anticipated exceptions (ArithmeticException and IOException). All other exceptions are permitted to propagate up the call stack.

...

Note that DivideByZeroException is a custom exception type that extends Exception.

Exceptions

...

EXC14-EX0: A catch block may catch all exceptions in order to process them before re-throwing them. For example:

...

  • Filtering out sensitive information from exceptions before the call stack leaves a trust boundary. Refer to guideline

...

...

...

...

...

...

...

...

In such cases, a catch block should catch Throwable rather than Exception or RuntimeException]. Consequently, an application may be required to catch _all_ exceptions at some appropriate level of abstraction to sanitize (or suppress) them. This is also summarized in the CWE entries, [CWE 7|http://cwe.mitre.org/data/definitions/7.html] and [CWE 388|http://cwe.mitre.org/data/definitions/388.html]. In such cases, prefer catching {{Throwable}} rather than {{Exception}} \[[Roubtsov 2003|AA. Bibliography#Roubtsov 03]\].

Wiki Markup
*EXC14-EX2*: Task processing threads such as worker threads in a thread pool or the swing event dispatch thread are permitted to catch {{RuntimeException}} when they call untrusted code through an abstraction such as {{Runnable}} \[[Goetz 2006 pg 161|AA. Bibliography#Goetz 06]\]. 

...

  • A realtime control system that catches and logs all exceptions at the outermost layer, followed by warm-starting the system so that realtime control can continue. Such approaches are clearly justified when program termination would have safety-critical or mission-critical consequences.
  • A system that catches all exceptions that propagate out of each major subsystem, logs the exceptions for later debugging, and subsequently shuts down the failing subsystem (perhaps replacing it with a much simpler, limited-functionality version) while continuing other services.

...

  • .

Risk Assessment

Catching RuntimeException traps several types of exceptions not intended to be caught. This prevents them from being handled properly.

...