Consider below code,
public class Test {
public void abc() {
try {
throw new OutOfMemoryError();
} finally {
System.out.println("finally");
}
}
public static void main(String[] args) {
new Test().abc();
}
}
Output :
Exception in thread "main" finally
java.lang.OutOfMemoryError
at Test.abc(Test.java:5)
at Test.main(Test.java:12)
So, finally
block is getting executed in the case, however above output is not fixed each time.
So the question is, Here we are throwing
OutOfMemoryError
andfinally
block is getting executed. Is this true for every case ?If yes then
finally
block will be executed whenOutOfMemoryError
will be thrown in reality, means memory area expansion will be performed while execution and enough memory not available ?