Thursday, August 29, 2013

Exception Handling - Examples.


/* Unchecked java.lang.ArithmeticException: / by zero will terminates the program*/

public class Exception_1
    {
        public static void main(String args[])
            {
                int a=5,b=0;
                System.out.println(a/b);
                System.out.println("\nThe - ArithmeticException() / divide by 0 will ");
            }
   }

Output : Following exception will be thrown at the line “System.out.println(a/b);”

java.lang.ArithmeticException: / by zero
          at Exception_1.main(Exception_1.java:8)

Users Defined Exception :

/*Userdefined Exception */
    public class Exception_2
        {
                public static void main(String args[])
                    {
                        int stock_qty = 500,Sold = 400;
                        System.out.println("\nCurrent Stock : "+stock_qty);
                        if (stock_qty < Sold)
                            {
                                ArithmeticException AE = new ArithmeticException("Stock is not Available");
                                throw AE;
                            }
                        else
                            {
                                stock_qty=stock_qty-Sold;
                                System.out.println("Product Sold and new Stock is "+stock_qty);
                            }
                   }
         }
Output: When,   Sold =400 :

Current Stock : 500
Product Sold and new Stock is 100

Output : Whem, Sold = 600 :

java.lang.ArithmeticException: Stock is not Available
          at Exception_2.main(Exception_2.java:10)

/* Input/Output Exception without try/catch Block */

import java.io.*;
    class Exception_3
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int Num;
                    System.out.print("Enter a Number : ");
                    Num=Integer.parseInt(br.readLine());
                    System.out.println("\nNumber Inputted : "+Num);
                }
        }        

Output:  If anything else than an integer value given as input –

Enter a Number : e

java.lang.NumberFormatException: For input string: "e"
          at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
          at java.lang.Integer.parseInt(Integer.java:447)
          at java.lang.Integer.parseInt(Integer.java:497)
          at Exception_3.main(Exception_3.java:10)
Output : When integer value given as input –

Enter a Number : 12

Number Inputted : 12


/* Input/Output Exception and FileNotFound Exception */

import java.io.*;
    public class Exception_4
        {
            public static void main(String args[])
                {
                    try {
                            File Fil = new File("Data.dat");
                            FileInputStream fis = new FileInputStream(Fil);
                            BufferedReader BR = new BufferedReader(new InputStreamReader(fis));
                            System.out.print("Enter a No. ");
                            int Num = Integer.parseInt(BR.readLine());
        
                        }
                   catch (FileNotFoundException FE)
                        {
                            System.out.println(FE);
                        }                         
                   catch (IOException IE)
                        {
                            System.out.println(IE);
                        }
                      
               }
      }

Output :

java.io.FileNotFoundException: Data.dat (The system cannot find the file specified)

/*Multiple Catch Block with Finally Block*/

public class Exception_5
        {
            public static void main(String args[])
                {
                    int Num=5;
                    try
                        {
                            Num= Num/0;
                        }
                    catch(Exception Exp)
                        {
                            System.out.println("Catch Block [cause : division by zero ]");
                        }
                    finally
                        {
                            System.out.println("Finally Block, Value of Num "+Num);
                        }
                    try
                        {
                            Num =6;
                            Num = Num/2;
                        }
                    catch(Exception Exp)
                        {
                            System.out.println("Catch Block [This one will not execute]");
                        }
                    finally
                        {
                            System.out.println("Finally Block, Value of Num "+Num);
        }
    }
}


Output :

Catch Block [cause : division by zero ]
Finally Block, Value of Num 5
Finally Block, Value of Num 3




No comments: