Exception Handling Required

Exception Handling Required


    Exception handling allows developers to detect errors easily without writing special code to test return values.

    An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:

    • A user has entered invalid data.

    • A file that needs to be opened cannot be found.

    • A network connection has been lost in the middle of communications, or the JVM has run out of memory.

    Without Exception Handling

     
                    
                 class  Manager
                 {
                    public static void main(String []args)
                    {
                       System.out.println(“Main Begin”);
                       String s1  = args[0];
                       System.out.println(“Input is  : ”+s1);
                       int I  =  Integer.parseInt(s1);
                       System.out.println(“Integer Parameter is ”+i);
                       int k  = (int)(i/(i-9));
                       System.out.println(k)
                       System.out.println(“End”);
                    }
                 }                
    Different Run Program:

        d: sdj_batch>java Manager

           Main Begin
           

        d: sdj_batch>java Manager 9

           

        d:\sdj_batch\java Manager praveen

           Main Begin
           Input is : 9
           Integer Parameter is 9
            For input string: "praveen"

        d:\sdj_batch\java Manager “18”

           Main Begin
           Input is : 18
           Integer Parameter is 18
           2
           End

    Why we required Exception Handling & Manage

    • The above program executes first three cases that causes the abnormal conditions.

    • To overcome those Abnormal Conditions sun people came up with one concept that is named as “ExceptionHandling”.

    • In java we can handle Exceptions in 2 ways they are
        1. Using “try & catch” blocks
        2. Using “throws” key word

    • Keep the statements inside “try” block which are producing abnormal condition and keep the “catch” block with corresponding abnormal condition Exception class.

    • In general if we receive any abnormal conditions then ctrl won’t reach to the end of the statementt, it will go away from where exception is raised.

    • By using try and catch, even if we receive the abnormal condition/flow, we can make it as normal flow means we can make ctrl reach to the last line of the code.

    • Catch block will take only one argument. We can’t pass more than one argument. Even though we can’t keep empty (or) no arguments in catch block.

    • We can keep one “try” block with more than one “catch” blocks.

    • Example 1:

       
                      
                   package com.sdj;
                   public class ExceptionDemo
                   {
                      public static void main(String[] args) 
                      {
                         System.out.println("Execution is start");
                         try
                         {
                            String strg=args[0];
                         }
                         catch(ArrayIndexOutOfBoundsExceptionaie)
                         {
                            System.out.println("pls enter atleast one cmdLine argument");
                         }
                         System.out.println("Execution End");
                      }
                   }                

      Output:

      Execution is start pls enter at least one cmdLine argument -- it is o/p from catch block Execution End
    • If there is any abnormal condition in try block then it can call the corresponding catch block.

    • Example 2:

       
                      
                   package com.sdj;
                   public class ExceptionDemo
                   {
                      public static void main(String[ ] args)
                      {
                         System.out.println("Execution is start");
                         try
                         {
                            String str=args[0];
                            int i=Integer.parseInt(str);
                            int result=i/(i-9);
                            System.out.println(“result:”+result);
                         }
                         catch(ArrayIndexOutOfBoundsExceptionaie)
                         {
                            System.out.println("pls enter at least one cmdLine argument");
                         }
                         catch(NumberFormatException ne)
                         {
                            System.out.println("pls enter numeric values only");
                         }
                         catch(ArithmeticException ae)
                         {
                            System.out.println("pls enter value more than 9");
                         }
                         System.out.println(“execution end”);
                      }
                   }                
      If we run the above program like below

          java ExceptionDemo

          Execution starts
          -- it is o/p from catch block with AIOBE means we are making abnormal flow as normal flow by using catch with AIOBE.
          Execution End

          java ExceptionDemoabc (here abc is passing as cmd line argmt)

          o/p:
          Execution starts
          -- it is o/p from catch block with NFE why bcz we are trying to converting String “abc” to int value its not possible.
          Execution End

          java ExceptionDemo9 (here 9 is passing as cmd line argmt)

          o/p:
          Execution starts
          -- it is o/p from catch block with AE.
          Execution End

        Here at line:4 we will receive I as 9 so result = i/(i-9) = 9/(9-9)= 9/0 = in java if we receive something by zero it cause the ArithmeticException. ,so from line:4 it will look for catch block with AE type ,then it will execute corresponding catch. Then it make it as normal flow

          java ExceptionDemo20 (here 20 is passing as cmd line argmt)

          o/p:
          Execution starts

          Execution End

      Example 3:

       
                      
                   package com.sdj;
                   public class ExceptionExpl
                   {
                      public static void main(String[] args)
                      {
                         String str=null;
                         int i=0;
                         int result;
                         System.out.println(“Execution Start”);
                         try
                         {
                            str=args[0];
                         }
                         catch (ArrayIndexOutOfBoundsExceptionaie) 
                         {
                            System.out.println("pls Enter at least one cmdLine argument");
                         }
                         try
                         {
                            i=Integer.parseInt(str);
                         }
                         catch(NumberFormatException ne)
                         {
                            System.out.println("pls enter numeric’s only");
                         }
                         try
                         {
                            result=i/(i-9);
                            System.out.println("result is:"+result);
                         }
                         catch(ArithmeticException ae)
                         {
                            System.out.println("pls enter any value except 9");
                         }
                         System.out.println("Execution end");
                      }
                   }                
      If we run the above program like below

         java ExceptionExpl

          Execution starts
          -- it is o/p from catch block with AIOBE, means we are making abnormal flow as normal flow by using catch with AIOBE.
          -- it is o/p from catch block with NFE, means we are making abnormal flow as normal flow by using catch with NFE.
          result is:0 -- it is o/p from try block with ArithmeticException
          Execution End

          Note
           if we run the the program like java ExceptionExpl then output as above . why because every abnormal condition will be handled by using try & catch blocks.
          java ExceptionExplguna(here guna is passing as cmd line argmt)

          Execution starts
          -- it is o/p from catch block with NFE means we are passing cmd line argmt as string type so for this abnormal flow we can make as normal flow by using try&catch block with NFE.
          result is:0 -- it is o/p from try block with ArithmeticException
          Execution End

         java ExceptionExpl 9(here 9 is passing as cmd line argmt)

          Execution starts
          -- it is o/p from catch block with AE.
          Execution End

          Execution starts
          result is:2 --

          Execution End

      Example 4:

       
                      
                   package com.sdj;
                   public class ExceptionExpl
                   {
                      public static void main(String[] args) 
                      {
                         try
                         {
                            String str=args[0];
                            int i=Integer.parseInt(str);
                            int result=i/(i-9);
                            System.out.println(result);
                         }
                         /*  System.out.println("Execution start");  */
                         /*  Here compilation fails why because in between try&catch  block  */
                         /*  we  don’t have any statements.  */
                         /*  A try block must followed by catch block only. */
                         catch (ArrayIndexOutOfBoundsExceptionaie)
                         {
                            System.out.println("catch block");
                         }
                      }
                   }                

      Example 5:

       
                      
                   package com.sdj;
                   public class ExceptionExpl 
                   {
                      public static void main(String[] args) 
                      {
                         String str=null;
                         try
                         {
                            str=args[0];
                         }
                         catch ( )
                         /*  Here compilation fails why because  without argument,  */
                         /*  catch block will not be possible. */
                         {
                            System.out.println("catch block");
                         }
                      }
                   }                

      Example 6:

       
                      
                   package com.sdj;
                   public class ExceptionExpl 
                   {
                      public static void main(String[] args) 
                      {
                         String str=null;
                         str=args[0];
                         catch (ArrayIndexOutOfBoundsExceptionaie)
                         /*  Here compilation fails because  without try block we can’t  */
                         /*  keep catch block which isn’t possible.  */
                         {
                            System.out.println("catch block");
                         }
                      }
                   }                
      • Even though if we have any abnormal condition flow inside catch block also then we can handle that flow as normal flow by using try & catch.

      • Inside catch block also we can keep try & catch blocks as like in below programs :

      Example 7:

       
                      
                   package com.sdj;
                   public class ExceptionExpl 
                   {
                      public static void main(String[] args)
                      {
                         String str=null;
                         try
                         {
                            str=args[0];
                         }
                         catch (ArrayIndexOutOfBoundsExceptionaie)
                         {
                            System.out.println("catch block");
                            try
                            {
                               int i=Integer.parseInt(str);
                               int result=i/(i-9);
                               System.out.println(result);
                            }
                            catch(NumberFormatException ne)
                            {
                               System.out.println("inside catch with try and catch");
                            }
                         }
                      }
                   }                

      Output:

      catch block inside catch with try and catch
      • Even though if we have any abnormal flow inside try block even then we can handle that flow as normal by using try &catch blocks.

      • Inside try block we can keep try & catch blocks also.

      Example 8:

       
                      
                   package com.sdj;
                   public class ExceptionExpl 
                   {
                      public static void main(String[] args) 
                      {
                         try
                         {
                            String str=null;
                            try
                            {
                               int i=Integer.parseInt(str);
                               int result=i/(i-9);
                               System.out.println(result);
                            }
                            catch(NumberFormatException ne)
                            {
                               System.out.println("inside try block with try and catch");
                            }
                         }
                         catch (ArrayIndexOutOfBoundsExceptionaie)
                         {
                            System.out.println("catch block");
                         }
                      }
                   }                

      Output:

      inside try block with try and catch
      Note 1
      • Try block will hold the Exceptional code.

      • Catch block will handle the Exceptions (or) handles the abnormal conditions.

      Note 2
      • If we get any abnormal condition then we should keep corresponding catch block for that abnormal condition. Then only the execution will reach the end of the block.

      • Else it will go away where abnormal condition raised. After that it won’t execute even single statement also.