Friday, May 14, 2010

Java Exceptions

Exceptions

* ArithmeticException -

public class ArithmeticException extends RuntimeException

- Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.

- ArithmeticException()
Constructs an ArithmeticException with no detail message.

-ArithmeticException(String s)
Constructs an ArithmeticException with the specified detail message.

* RuntimeException

public class ClassCastException extends RuntimeException

- Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);

System.out.println((String)x);
- ClassCastException()
Constructs a ClassCastException with no detail message.
-ClassCastException(String s)
Constructs a ClassCastException with the specified detail message.
* IndexOutOfBoundsException
public class IndexOutOfBoundsException extends RuntimeException

- Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

- Applications can subclass this class to indicate similar exceptions.
- IndexOutOfBoundsException()
Constructs an IndexOutOfBoundsException with no detail message.
- IndexOutOfBoundsException(String s)
Constructs an IndexOutOfBoundsException with the specified detail message.
* NullPointerException
public class NullPointerException extends RuntimeException
- Thrown when an application attempts to use null in a case where an object is required. These include:
o Calling the instance method of a null object.
o Accessing or modifying the field of a null object.
o Taking the length of null as if it were an array.
o Accessing or modifying the slots of null as if it were an array.
o Throwing null as if it were a Throwable value.
- Applications should throw instances of this class to indicate other illegal uses of the null object.
- NullPointerException()
Constructs a NullPointerException with no detail message.
- NullPointerException(String s)
Constructs a NullPointerException with the specified detail message.
* FileNotFoundException

public class IOException extends Exception
- Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
* ConcurrentModificationException
- This exception may be thrown by methods that have detected concurrent modification of a backing object when such modification is not permissible, e. g. two threads modifying a HashMap simultaneously.
* NullPointerException
- Actually a null reference exception.
* StringIndexOutOfBoundsException
- Can be handled more generically with IndexOutOfBoundsException.
* NumberFormatException
- Commonly thrown when a String is converted to internal binary numeric format.
* ArrayStoreException
- Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.