Jtest logo




Contents  Previous  Next  Index

PB.NDC


Never define a direct or indirect subclass of class Error, RuntimeException or Throwable

Description

This rule flags code that defines a direct or indirect subclass of class Error, RuntimeException, or Throwable.

The class java.lang.Error is meant for covering abnormal Java Virtual Machine conditions only. If you define a direct or indirect subclass of class java.lang.Error, it is implied that the error is also an abnormal Java Virtual Machine condition, which is not the case. Exception handling of java.lang.Error is not checked by the Java compiler, so erroneous exception handling might not be noticed.

Exceptions in java.lang.RuntimeException and its subclasses are used for avoidable exceptions. The Java compiler does not check the correct handling of these exceptions, so erroneous exception handling might not be noticed.

The class java.lang.Throwable is the super class of java.lang.Exception and java.lang.Error. User-defined exceptions should always be defined as subclasses of java.lang.Exception.

Example

 package PB;
          
 public class NDC_Exception extends RuntimeException { // violation
          
     public NDC_Exception (String s) {
         super(s);
     }
 }

Repair

 package PB;
          
 public class NDC_Exception extends Exception {
          
     public NDC_Exception (String s) {
         super(s);
     }
 }

Reference

Daconta, M, Monk, E, Keller, J, and Bohnenberger, K. Java Pitfalls. John Wiley & Sons, 2000, pp.12 - 14.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft