3

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.

  1. So the question is, Here we are throwing OutOfMemoryError and finally block is getting executed. Is this true for every case ?

  2. If yes then finally block will be executed when OutOfMemoryError will be thrown in reality, means memory area expansion will be performed while execution and enough memory not available ?

5
  • 6
    That's right. And another OutOfMemoryError may be thrown inside the finally block which will supersede the other one. Commented Apr 9, 2014 at 16:50
  • In that case eclipse warning is : "finally block does not complete normally". Commented Apr 9, 2014 at 16:54
  • @KishanSarsechaGajjar - that error appears s because you're explicitly throwing the exception. In the real world it just happens. Commented Apr 9, 2014 at 17:15
  • And to add to what @SotiriosDelimanolis said - if a second OOM -- or any other exception -- happens in the middle of your finally, it means that your finally won't be completely executed. Commented Apr 9, 2014 at 17:16
  • 1
    The output is not 'fixed' because the message finally gets written to standard output but the stacktrace gets written to standard error. Try System.err.println("finally") instead. Commented Apr 9, 2014 at 17:27

2 Answers 2

2

Things will get interesting if you try to do something in the finally block requiring additional memory:

public class Finally {

    public void abc() {
        try {
            throw new OutOfMemoryError();
        } finally {
            byte[] b = new byte[1024*1024*1024];
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        new Finally().abc();
    }
}

Now when you execute the code (java -Xmx20m Finally for example), you can easily end up in a situation where your available heap has been exhausted to the extent where the finally block cannot fully complete, thus its execution cannot be guaranteed. So I would not recommend to rely on finally blocks in case of OutOfMemoryErrors.

Sign up to request clarification or add additional context in comments.

Comments

2

that's right like said Sotirios Delimanolis

and more surprisingly even if you put a return in your try block the finally will be executed

From the doc :

"finaly block allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated"

check this for more information http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

7 Comments

There is a SO question on return. Please explain in context of OutOfMemoryError.
Looks pretty explanatory. In case of OOM, the finally block will be executed.
@GiovanniBotta "more surprisingly even if you put a return..." It is not surprising at all...I know that already. I want to know behavior in context of OutOfMemoryError not in context of return statement.
@KisHanSarsecHaGajjar I think OOM is no different from any other error/exception. The only way the finally block will not execute is if you call System.exit() or the whole JVM has already crashed, which in all fairness could happen when OOM happens on some platforms. However, I have seen JVMs resiliently surviving OOM, although they would behave unpredictably.
@GiovanniBotta What about for(;;){} ? (PS : I found this on same question you provided)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.