Write a Finally block Program

Write a Finally block Program

Program
public class Main
{
    public static void main(String args[])
    {
        int x,y,z;
        System.out.println("Enter x,y values :");
        Scanner s=new Scanner (System.in);
        try
        {
            x=s.nextInt();
            y=s.nextInt();
            z=x/y;
            System.out.println("z= "+z);
            //System.out.println("hello");
        }
        catch(ArithmeticException ae)
        {
            System.out.println("Denominator should not be zero");
        }
        finally
        {
            System.out.println("hello");// try block statement
        }
    }
}

Output:

Enter x,y values :10 2
z= 5
hello

Enter x,y values :10 0
Denominator should not be zero
hello


In the above example either exception is raised or not raised the statements of finally block executed .
Note: the statements whatever is executing with in finally block these are generally try block statements.


Exception Handling In Java



More Questions


169 . Write a Finally block Program
170 . Write a program to handle negative age exception
171 . Write a program to handle negative salary exception
172 . Write a Simple interest program to handle negative amount and duration and interest using user defined exception
173 . Write a String length program
174 . Write a string charAt() method program
175 . Write a string toLowerCase() Program
176 . Write a String toUpperCase() program
177 . Write a string concat() program
178 . Write a string compareTo program
179 . Write a string compareToIgnoreCase() Program