Java Exception Handling

Home  »    Java   »    Advance Java  »  Exception Handling

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Exceptions can be thrown by the system or explicitly thrown by the program. Exceptions include errors that could be fatal to a program, and may also include other unusual situations. By managing exceptions, one can manage errors and possibly work around them.

Exceptions in Java are actual objects. They are instances of classes that are inherited from the class Throwable. An instance of a Throwable class is created when an exception is thrown.

The below diagram shows a partial class hierarchy for exceptions.



Throwable has two subclass Error and Exception class. Instances of Error class are internal errors in Java runtime environment( the virtual machine). These error are rare and are usually fatal.

Suvclass of Exception fall into two general groups:

  1. Runtime exceptions (subclasses of the class RuntimeException) such as ArrayIndexOutofBounds, SecurityException, or NullPointerException.
  2. Other exceptions such as EOFException and NullPointerException.





Catching Java Exceptions

The following two steps need to be performed to trap an exception:

  1. Protect the code that contains the method that might throw an exception inside a try block.
  2. Test for and deal with an exception inside a catch block.


What try and catch effectively means is "try" the code that might cause an exception. If it executes, go on with the program. If it does not execute catch the exception and deal with it".

The catch block should immediately follow the try block.

Example
The following algorithm can explain the try and catch method.



The above example explains the algorithm of opening a file. The opening of the file could potentially throw an exception. To handle this exception, the code is enclosed inside a try block.

As the try block is likely to throw any file related error, an associated catch block traps a file related exception.

The following example is an applet called ExecpApp with two text fileds, When a number is entered in each of the text fields and the Enter key is pressed, the program divides the first number by the second and display the result (dropping any remainder). the following example does not contain any exception-handling code.




C:\>jdk1.4\bin>javac ExcepApp.java
C:\>jdk1.4\bin>appletviewer ExcepApp.html



"Output of ExcepApp.class"

Incase the user presses the Entery key when either of the text boxes are empty or enteres an invalid numerical value, then Java throws a NumberFormatException.





Incase of Division by zero, refer the below diagram for th ArithemeticException raised.




"Division by zero"



Arithmetic Exception raised incase of division by zero

Incase of a valid number being entered, the result after the division of numbers is shown in below diagram.
















Incase of a valid number entered













































































Catching Java Exceptions Catching a RunTime Exception
Handling Multiple Exceptions The finally Clause
Creating User-defined Exceptions



<< Previous Topic
Next Topic >>



4 comments:

  1. Java Exception Handling method have easy understood me.Thanks.
    Java Training with Placements

    ReplyDelete

  2. I really appreciate this stuff.it is very amazing content.thanks for sharing such a useful stuff.it really helped me very much to solve many proble.


    Aluminium Telescopic ladders manufacturers

    ReplyDelete