Exception Handling Definition

Exception Handling


    While Executing the program if we receive any abnormal conditions then program will terminate, there itself, it’s called abnormal execution, Those abnormal conditions are called as “Exceptions ”.

    For every abnormal conditions sun people provided corresponding Exception classes.
    • Once if we receive any abnormal condition then sun people will invoke corresponding Exception class.
      Example :Some of the Exception classes are
        1. java.lang.ArrayIndexOutOfBoundsException
        2. java.lang.NumberFormatException
        3. java.lang.ArithmeticException

    Example 1:

     
                    
                 package com.sdj;
                 public class ExceptionDemo
                 {
                    public static void main(String[] args) 
                    {
                       /*  String str=args[0];  */
                       /*  Here exception will be raised i.e.,   */
                       /*  java.lang.ArrayIndexOutOfBoundsException. why because  */ 
                       /*  we are not  passing any cmdline argument. */
                       System.out.println ("input string is:"+str);
                       int i=Integer.parseInt(str);
                       System.out.println("input as int format"+i);
                       System.out.println(i/(i-5));
                    }
                 }                
    If we run the above program like this :

        java ExceptionDemo

      Output:

           
           
       why because we are expecting atleast one cmdLine argument at line: 6. But we are not passing any cmdLine argument.

        java ExceptionDemoabc (here ‘abc’ is act as cmdLine arg to our prog)

      Output:

           for input string is: abc
           
           
               
               
               

      Note
      • While running the above program we will get java.lang.NumberFormatException.

      • why because we are passing String value as “abc” but we are converting String to Integer value. So we will get NumberFormatException .i.eparseInt() will raise the exception if input String ‘abc’ is not numeric value.

    If we run the above program like this :

        java ExceptionDemo 5 (here ‘5’ is act as cmdLine arg to our prog)

      Output:

           input string is:5
           input as int format5
           
           

      Note

        If we run the EXAMPLE1 by passing cmdline as java ExceptionDemo 5 then we will get java.lang.ArithmeticException at line no:10. Why because we are passing the value 5. So at line 10 at Ex1 is “i/(i-5)” becomes like as “5/(5-5)”=5/0

    If we run the above program like this :

        java Exception demo 10 (here ‘10’ is act as cmdLine arg to our prog)

      Output:

           input string is:10
           input as int format: 10
           2

    Exception Handling Block:

    • try,catch
    • try,catch,finally
    • try,catches,finally
    • try and finally —→ not come under exception handling .but is ensuring that try block is executes without catch. It is only possible to put try without catches if only one case you put finally with try.