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!