Question

public class Confusion {
    Confusion(int i) {
        int j = 5;
        int[] a = new int[2];
        try {
            a[0] = 4;
            if (i <= 0) {
                int k = j / i;
            } else {
                System.out.println(j / i);
            }
        } catch (ArithmeticException sa) {
            System.out.println("Wrong value" + sa);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("out of range massage in Class");
        } finally {
            System.out.println("Executing finally block in code");
        }
    }

    void k() {
        int[] a = new int[2];
        {
            try {
                a[4] = 4;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("out of range");
            }
        }
    }
}

public class Nested {
    public static void main(String[] args) {
        Confusion c = new Confusion(2);
        Confusion c1 = new Confusion(0);
        c1.k();
        c.k();
    }
}

Output:

-2
Executing finally block in code
Wrong valuejava.lang.ArithmeticException: / by zero
Executing finally block in code
out of range
out of range

Whenever i am executing the finally{} block written in the code below it is getting executed twice. Don't know why this is happening. I want to run the finally block only once. Is there any way to throw multiple exception in single method?

Was it helpful?

Solution

It because you have two Confusion objects. Therefore, the constructor will be executed twice.

confusion c=new confusion(2);
confusion c1=new confusion(0);

OTHER TIPS

confusion c=new confusion(2);
confusion c1=new confusion(0);

thats why u are getting 2 outputs from finally.

This is because you are creating two objects c1 and c

The finally block always executes when the try block exits.

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

So regardless of any exception it will execute finally block. As you create two objects, it will call the constructor twice and will also execute finally twice.

You are calling the code in the try twice.

confusion c=new confusion(2);
confusion c1=new confusion(0);

This means that as the finally happens every time the try is called, it'll print...

Executing finally block in code

If you called it again,

confusion c1=new confusion(3);

It'd print out the contents of the finally a third time.

You are creating two objects using following code. Thats why the finally block is executed two times.

confusion c=new confusion(2);
confusion c1=new confusion(0);

Try this one please

public static void main(String[] args){
   confusion c=new confusion(2);
   confusion c1=new confusion(0);
   confusion c1=new confusion(10);
   confusion c1=new confusion(5);
} 

Now finally block called 4 times.

Constructor in Java is block of code which is executed at the time of Object creation.

Please read constructor reference http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Because you are creating two Confusion objects. Therefore, the finally block will be executed twice.

confusion c=new confusion(2);
confusion c1=new confusion(0);

If you want to execute finally only once.Try this code

 public class Confusion {
    Confusion(int i) {
        int j = 5;
        int[] a = new int[2];
        a[0] = 4;
        if (i <= 0) {
           int k = j / i;
        } else {
           System.out.println(j / i);
        }
    }

    void k() {
       int[] a = new int[2];
            a[4] = 4;
   }
}

public class Nested {
   public static void main(String[] args) {
      try{
           Confusion c = new Confusion(2);
           Confusion c1 = new Confusion(0);
           c1.k();
           c.k();
         }catch (ArithmeticException sa) {
            System.out.println("Wrong value" + sa);
         } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("out of range massage in Class");
        } finally {
            System.out.println("Executing finally block in code");
        }
     }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top