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 } } }
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.