Write about Exception Handling concept in Java with Example?

Exception:

An exception is a condition that is caused by a run –time error in the program. when the java interpreted encounters an error such as dividing an integer by zero. It creates an exception object is not caught is handled properly the interpreter will displayed an error message is will terminate the program .If we want the program to continue with the execution of the remaining code then we should try to catch the exception object thrown by the error condition and then display an appropriate message for taking corrective actions.           

1)Find the problem (hit the exception),

2) Inform that an error has occurred (throw the exception),

3)Receive the error information ( catch the exception),

4)Take corrective action (handle the exception)

The error handling code basically consists of two segments. One to detect errors is to throw exception and the other to catch exception and to take appropriate action.

            Exception Type
            Cause of Exception
1)      Arithmetic exception
2)      Array index out of bound’s exception
3)      File not found exception
4)      Io exception


5)       Null pointer exception
6)      Number format exception
7)       Out of memory exception


           
1)  caused by math errors such as division, by       zero
2)  caused by bad array indexes
3) caused by an attempt to access a non-          existent file
4) caused by general I 10 failures , such as    inability to read from a file
5)  caused by referencing a null object
6) caused when a conversion b /w string   number number
7)  caused when here is not enough memory  to allocate a new object


Syntax of Exception Handling code:

The basic concept of exception handling are throwing an exception and catching it             
                       
exception handling

Java uses a keyboard try to preface a block of code i.e; likely to causes an error condition and “throw” an exception. A catch block defined by the keyboard catch “catches” the exception “thrown” by the try block and handles it appropriately. The catch block is added immediately.

            -------
            try
                {
                  Statement; // generates an exception.
}
            catch (Exception type)
                {
                 Statement ; // process the exception
                }
             ------
             ------

The try block can have one as more statement that could generate an exception. If anyone statement generates an exception, The remaining statement in the block are skipped and execution jumps to the catch. That is placed next to the try block.
The catch statement block too can have one or more statement that are necessary to process the exception remembers that every try statement should be followed by at least one catch statement.

The catch statement works like a method definition. The catch statement is passing a single parameter which is reference to the exception object thrown. If the catch parameter matches with the type of exception object, then the exception is caught and statement in the catch block will be executed otherwise .The exception is not caught and the default exception handles will cause the exception to terminates.

Ex: class error 3                      
  {                                                                   
   public static void main (String args[ ])                              
     {                                                         
         int a = 10;                                          
         int b=5 ;
         int c=5 ;
         int x,y ;
         try
           {
              x=a / (b-c);
           }
        catch (ArithmeticException  e)
           {
               System.out.println (“ Division by zero”) ;
            }
          y= a/(b+c);
         System.out.println (“y= “+y);
        }
  }

OutPut: Division by zero
                         y=1.   

Multiple Catch Statements:

It is possible to have more than one catch statement in the catch block as illustrated below
    ------
    ------
       try
         {
            Statement ; // generates an exception
          }                 
      catch (exception – type1 e)
          {
             Statement; // processes exception type 1               
           }
       catch (exception –type2 e)
           {
              statement; // process exception type 2
            }
        catch (exception- typeN e)
            {
               Statement ;
             }
           -----
           -----
 Exception in a try block is generated the Java treats the multiple catch statement like, classes in a switch statement. The first statement whose parameter match with the exception object will be executed and the remaining statement will be skipped.

Using Finally Statement :

Java supports another statement known as finally statement that can be used to handle on exception i.e, is not caught by any of the previous catch statements, finally block can be used to handle any exception generated with in a try block . It may be added immediately after the try block or after the catch block shown as follows.           

            try                                          try
            {                                            {
             -----                                        -----     
             -----                                        -----
             }                                             }
             finally                                   catch(----)
             {                                             {
              -----                                        -----
              -----                                        -----    
              }                                            }
                    catch(----)       
                    {
                     -----
                     -----
                    }
                    finally
                    {
                     -----
                     -----
                     }

When a finally block is defined this guaranteed to executed, regardless of whether or not in exception is thrown. As a result, we can use at to perform certain house. Keeping operation such as using files and releasing system resources.
                                                                                                           
















Related

Java 6070680097421435003

Post a Comment

emo-but-icon

item