Thursday, January 13, 2011

Exceptions in Java

Summary: This module explains the various exceptions that are raised by the Java Runtime Environment.



Out of range

These exceptions are the most common and are caused by attempting to access an array or string with an index that is outside the limits of the array or string. C programmers will be familiar with the difficult bugs that are caused by such errors which “smear” memory belonging to other variables; in Java the errors cause an exception immediately when they occur thus facilitating debugging.

ArrayIndexOutOfRange

This exception is thrown when attempting the index an array a[i] where the index i is not within the values 0 and a.length-1, inclusive. Zero-based arrays can be confusing; since the length of the array is larger than the last index, it is not unusual to write the following in a for-statement by mistake:
final static int SIZE = 10;
int a = new int[SIZE];
 
for (int i = 0; i <= SIZE; i++)     // Error, <= should be <
 
for (int i = 0; i <= a.length; i++) // Better, but still an error

StringIndexOutOfRange

This exception is thrown by many methods of the class String if an index is not within the values 0 and s.length(), inclusive. Note that s.length() is valid and means the position just after the last character in the string.

NullPointerException

A variable in Java is either of a primitive type such as int or boolean or of a reference type: an array type, the type of a class defined in the Java API such as String, or the type of a class that you define such as MyClass. When declaring a variable of a reference type, you only get a variable that can hold a reference to an object of that class. At this point, attempting to access a field or method of the class will cause the exception NullPointerException to be thrown:1
class YourClass {
  static MyClass my;    // Can hold a reference but doesn't yet
 
  public static void main(String[] args) {
    my.m(5);            // Throws NullPointerException
  }
}
Only after instantiating the class and executing the constructor do you get an object:
class YourClass {
  static MyClass my;    // Can hold a reference but doesn't yet
 
  public static void main(String[] args) {
    my = new MyClass(); // Instantiates an object
                        //   and assigns the reference
    my.m(5);            // Throws NullPointerException
  }
}
This exception is not often seen in this context because Java style prefers that the declaration of the variable include the instantiation as its initial value:
class YourClass {
  // Declaration + instantiation of class variable
  static MyClass my1 = new MyClass();
 
  public static void main(String[] args) {
    my1.m(5);            // OK
    // Declaration + instantiation of local variable
    MyClass my2 = new MyClass();
    my2.m(5);            // OK
  }
}
Sometimes, you cannot initialize a variable at its declaration; an obvious place to initialize an instance variable is within its constructor:
class YourClass {
  MyClass[] my;          // Can't initialize here because ...
 
  YourClass(int size) {  // ... size of array different for each
    object my = new MyClass[size];
  }
}
The exception is likely to occur when you declare an array whose elements are of reference type. It is easy to forget that the array contains only references and that each element must be separately initialized:
MyClass[] my = new MyClass[4];       // Array of references
my[1].m(5);                          // Raises an exception
 
for (int i = 0; i < my.length; i++)
  my[i] = new MyClass();             // Instantiate objects
my[1].m(5);                          // OK
Finally, NullPointerException will occur if you get a reference as a return value from a method and don't know or don't check that the value is non-null:2
Node node = getNextNode();
if (node.key > this.key) ...         // Error if node is null!
 
if (node != null) {                  // This should be done first
  if (node.key > this.key) ...
}

Computational errors

The following three exceptions occur when you try to convert a string to a number and the form of the string does not match that of a number, for example "12a3".

InputMismatchException

This exception is thrown by the class Scanner, which is a class introduced into version 5 of Java to simplify character-based input to programs.

IllegalFormatException

This exception is thrown by the method format in class String that enables output using format specifiers as in the C language.

NumberFormatException

This exception is thrown by methods declared in the numeric “wrapper” classes such as Integer and Double, in particular by the methods parseInt and parseDouble that convert from strings to the primitive numeric types.

ArithmeticException

This exception is thrown if you attempt to divide by zero.
Important note
Most computational errors do not result in the raising of an exception! Instead, the result is an artificial value called NaN, short for Not a Number. This value can even be printed:
double x = Math.sqrt(-1);  // Does not throw an exception!
System.out.println(x);
Any attempt to perform further computation with NaN does not change the value. That is, if x is NaN then so is y after executing y = x+5. It follows that an output value of NaNgives no information as to the statement that first produced it. You will have to set breakpoints or use print statements to search for it.

Insufficient memory

Modern computers have very large memories so you are unlikely to encounter these exceptions in routine programming. Nevertheless, they can occur as a side effect of other mistakes.

outOfMemoryError

This exception can be thrown if you run out of memory:
int a = new int[100000000];

StackOverFlowArea

A stack is used to store the activation record of each method that is called; it contains the parameters and the local variables as well as other information such as the return address. Unbounded recursion can cause the Java Virtual Machine to run out of space in the stack:
int factorial(int n) {
  if (n == 0) return 1;
  else return n * factorial(n + 1);  // Error, you meant n - 1
}

FOOTNOTES

  1. The name of the exception is an anachronism, because there are no (explicit) pointers in Java.
  2. You could know this if the method has been verified for a postcondition that specifies a non-null return value.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

java

Popular java Topics