Thursday, April 12, 2018

In which case finally will not executed?

Statements in which finally block is not executed are following:

if you call System.exit()
if the JVM crashes first
if there is an infinite loop in the try block
if the power turns off
Thread.currentThread().suspend();
OS kills JVM process.
Runtime.getRuntime().exit(0);
Runtime.getRuntime().halt(0);


Testing the finally block in different statement in try block.

 public static void main(String [] args){

    try{
        System.out.println("Before Statement");
        /*** Statement ***/
        System.out.println("After Statement");
    }
    catch(Exception e){
    }
    finally{
        System.out.println("Finally is Executed");
    }

Statements in which finally block is executed are following:

Thread.currentThread().interrupted();
Thread.currentThread().destroy();
Thread.currentThread().stop();
Thread.sleep(10);
Thread.currentThread().interrupt();
Runtime.getRuntime().addShutdownHook(Thread.currentThread());
If there is any exception occurred.
If there is no exception.

No comments:

Followers

Link