-1

I have code like this:

class ExceptionTest{

    public Integer divide(int a, int b) {
        try {           
            return a/b;         

        }finally {
            System.out.println("Finally");
        }
   }


}

public class Three {

    public static void main(String[] args) {


        ExceptionTest test = new ExceptionTest();

        try {
            System.out.println(test.divide(10, 0));         
        }catch(Exception e) {
            System.out.println("DIVIDED BY 0!");
        }
   }
}

When I run the code it prints:

Finally 
DIVIDED BY 0!

Why is that? If there Exception has been caught shouldn't only "DIVIDED BY 0!" be printed?

EDIT:

I know that finally is always printed, but what I mean is that try-finally in the method called "divide" is called in try-catched block in main. So If exception is being caught why something from try in try-catch is printed? Even if my ExceptionTest class looks like this:

class ExceptionTest{

    public Integer divide(int a, int b) {                   

           System.out.println("Finally")

            return a/b;     
   }


}

So there is no try-finally block and exception is being thrown and I still have

Finally
Divided by 0!
Gregory
  • 65
  • 6
  • 2
    The whole point of a `finally` block is to absolutely guarantee that the code therein is run. – azurefrog Feb 01 '18 at 16:54
  • finally will allways be executed. That's the whole point of having that block type. – OH GOD SPIDERS Feb 01 '18 at 16:54
  • Can you explain why you think `finally` should not be executed? – Boris the Spider Feb 01 '18 at 16:55
  • @BoristheSpider I've edited my question to be more specific, and I don't think that this is the duplicated question as you marked. – Gregory Feb 01 '18 at 17:18
  • 1
    @Gregory Your edit doesn't make that much sense because there you have the `System.out.println("Finally")` before you do the division. So obviously the printing will be executed before the exception is thrown. The exception happens at `a/b` and that's the moment your method stops with an exception. – OH GOD SPIDERS Feb 02 '18 at 09:02

2 Answers2

0

The Docs tell us about the finally-block

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

So even if you divide by 0, which raises an Exception, the finally block will be reached.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
0

The code within the finally block is always run, even if the code executes correctly. You need to use catch instead, which is only executed when an error occurs.

try { /* Runs first, and stops executing if an Exception's thrown. */  }
catch(Exception e) { /* Run whenever an Exception's thrown. */ }
finally { /* Always runs. */ }

You also need to make sure that you return a value from the catch clause. Something like

try {
    return a / b;
} catch (ArithmeticException ae) {
    System.err.println("Cannot divide by zero!");
    return -1;
}
Lane Surface
  • 56
  • 1
  • 6