Saturday, 19 October 2013

Program for multiple catch and finally

//Program for multiple catch and finally
//Developed by Ahlam Ansari

import java.util.*;
class MultiCatchFinal
{
    public static void main(String ahlam[])
    {
        int calendar[]=new int[31];
        int last,max,scale;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the max and scale value");
        max=sc.nextInt();
        scale=sc.nextInt();
        try
        {
            last = calendar[max/scale] ;
        }
        catch (ArithmeticException e)
        {
            System.out.println("Division of "+max+" by "+scale+" cannot be carried out.");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("The index "+max/scale+" is out of bounds");
        }
        finally
        {
            System.out.println("This is Final Block");
        }
        System.out.println("This is Main Block");
    }
}

/*
Output:

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MultiCatchFinal.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MultiCatchFinal
Enter the max and scale value
1221
31
The index 39 is out of bounds
This is Final Block
This is Main Block

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MultiCatchFinal
Enter the max and scale value
21
0
Division of 21 by 0 cannot be carried out.
This is Final Block
This is Main Block

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment