-5

If I have the following code

try {
    //some stuff here
catch (Exception e) {
    throw new CustomException();
} finally {
    finalize();
}

where CustomException is a checked exception,

Will the finally block be called in the event of an Exception?

EDIT: Perhaps "finalize()" was a poor choice of wording. I DID NOT mean the finalize() method of Object. I just meant that there was cleanup code that needed done. Apologies for that.

jonbonazza
  • 926
  • 2
  • 10
  • 16
  • 1
    `finally` block is **always** called. – SJuan76 Oct 31 '13 at 15:18
  • Yes, but you'll have to either catch the `CustomException` or declare it as being thrown from the enclosing method. – superEb Oct 31 '13 at 15:18
  • 2
    There was a much easier way for you to find out. – Sotirios Delimanolis Oct 31 '13 at 15:18
  • @SJuan76 Almost always. Java could crash before it's called... – Shaded Oct 31 '13 at 15:19
  • @Shaded Or someone could pull the plug from the computer, or a volcano might erupt and bury the computer under lava before the statement is reached etc... – Jesper Oct 31 '13 at 15:35
  • @Jesper Exactly! At least I'm not alone in believing volcanos are out to get me. – Shaded Oct 31 '13 at 15:43
  • Though you should never invoke `finalize()` on an object anyway. In most cases you don’t need a `finalize()` method but if you need it, it will be called by the JVM automatically. Invoking it by yourself means risking `finalize()` beeing called twice, once by your code and once by the JVM. – Holger Oct 31 '13 at 16:48
  • possible duplicate of [Does finally always execute in Java?](http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java) – Dennis Meng Oct 31 '13 at 20:27

6 Answers6

2

Finally is ALWAYS called, regardless of exceptions and even return statements.

I recommend checking the Documentation on it.

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
1

It will always be called in the event of an exception. For that matter, it will always be called, period. That's what it's for.

MadConan
  • 3,749
  • 1
  • 16
  • 27
1

finally is always called(even if you call return;), unless you call

System.exit();

in try or catch block.

John
  • 576
  • 1
  • 6
  • 14
0

finally block is called in each cases.

k4sia
  • 414
  • 1
  • 6
  • 18
0

The finally block is always executed. It is designed so that you can perform important operations regardless of any exceptions being thrown, for example close streams.

Blady
  • 1
0

Yes, finally is ALWAYS executed, no matter what happens, short of the JVM dying before your code is reached. It will even get called in this case:

public boolean doSomething() throws Exception{
    boolean iDidIt = true;
    try{
        //attempt to do it
        return iDidIt;
    catch(Exception e){
        iDidIt = false;
        return iDidIt;
    } finally {
        System.out.println("Did something get done? "+iDidIt);
    }
}

The print statement will get printed no matter what.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36