The term exception denotes an abnormal event or unexpected situations.
Exception-handling is needed to handle unexpected situations like:
- inability to find files
- problems in network connectivity
- running out of memory
- resource allocation errors
- The class at the top of the exception hierarchy is the Throwable class
- It is a part of the java.lang package
Two classes derived from it are:
- Error – defines the conditions that do not occur under normal circumstances.
- Exception – defines abnormal conditions that have to be trapped in a program.


Note: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
There are two types of exceptions in Java:
- unchecked exceptions and
- checked exceptions.
Unchecked exceptions are any class of exception that extends the RuntimeException class at some point in its inheritance hierarchy.
This includes the most common exceptions.
- An ArithmeticException occurs when a program tries to divide by zero.
- A NullPointerException occurs when you try and access an instance data member or method of a reference variable that does not yet reference an object.
- Unchecked exceptions can occur anywhere in a program and in a typical program can be very numerous.
- Therefore, the cost of checking these exceptions can be greater than the benefit of handling them.
Note: All Exceptions that extend the RuntimeException class are unchecked exceptions.

- Checked exceptions are exceptions that do not extend the RuntimeException class.
- Checked exceptions must be handled by the programmer to avoid a compile-time error.
- One example of a checked exception is the IOException that may occur when the readLine method is called on a BufferedReader object.
Note:All Exceptions that DO NOT extend the RuntimeException class are checked exceptions.

Checked Exception

Errors at compile time : here IOException is checked exception.
When an unexpected event occurs in a method, Java creates an object of the type Exception
- Java sends the object to the program, by an action called throwing an exception
- The Exception object contains information about the type of error and the state of the program when the exception occurred.
- To handle the exception an exception-handler is created, which processes the exception.
Keywords Used in Exception handling:
- The try
- The catch
- The throw
- The throws
- The finally
It Guards the statements that may throw an exception.
Syntax
try
{
//statements that may cause an exception
}
Is used to catch any exceptions thrown from the try block
- The scope of the catch block is restricted to the statements in the preceding try block
- The catch statement takes a parameter which is the object of the Exception class that refers to the exception caught
- One try block can have multiple catch blocks
Syntax
try
{
//statements
//that may cause an //exception
}
catch(...)
{
//error-handling routines
}

Multiple catch

Multiple catch with Exception Class

Problem in Multiple catch with Exception Class

- Is used for throwing an exception.
- Takes a single argument, which is an object of the Exception class
- Is commonly used in programmer-defined exceptions
Syntax
throw ThrowableInstance;

- Is used to specify that an exception has to be handled by the calling method
- Is used for an exception that a method is capable of raising but not handling
Syntax
[<access_specifier>][<modifier>]<return_type> <method_name>>(<arg_list>)
[throws <exception_list>]
throws Keywords Example

Is used when it is necessary to process certain statements no matter whether an exception is raised or not
Syntax
finally
{
//statements to be executed
}
The usage of the finally block:
try
{
opening File();
working with File(); // exception may occur
}
catch(....)
{
//process the exception code
}
finally
{
File should close();
}
try..catch..finally Example

try..finally Example

- ArthmeticException Exception – is thrown when an exceptional arithmetic condition has occurred
- NullPointerException Exception – is thrown when an application attempts to use NULL where an object is required
- ArrayIndexOutOfBoundsException Exception – is thrown when an attempt is made to access an array element beyond the index of the array.
- NumberFormatException Exception – if it is not a number
- ClassNotFoundException – thrown if a program can not find a class it depends at runtime (i.e., the class’s “.class” file cannot be found or was removed from the CLASSPATH)
- IOException – actually contained in java.io, but it is thrown if the JVM failed to open an I/O stream
The catch statement also stores an instance of the exception that was caught in the variable that the programmer uses, in the previous example Exception e. While all exceptions are subclasses of Exception, Exception itself is a subclass of Throwable, which contains a nice suite of methods that you can use to get all kinds of information to report about your exceptions:
- getMessage() : returns the error message reported by the exception in a String
- printStackTrace() : prints the stack trace of the exception to standard output, useful for debugging purposes in locating where the exception occurred
- printStackTrace(PrintStream s) : prints the stack trace to an alternative output stream
- printStackTrace(PrintWriter s) : prints the stack trace to a file, this way you can log stack traces transparent to the user or log for later reference toString()–if you just decide to print out the exception it will print out this: NAME_OF_EXCEPTION: getMessage().
Recent Comments