Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any code, that will never execute finally clause? [duplicate]

Tags:

java

Possible Duplicate:
Is there such case when in try\finally block the finally won't be executed?

is there any code, that will never execute finally clause?

like image 693
developer Avatar asked Apr 07 '11 10:04

developer


2 Answers

Java Tutorials

Not only for System.exit , but also for thread interrupted

Note: If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.

like image 186
Dead Programmer Avatar answered Nov 15 '22 19:11

Dead Programmer


System.exit(0) is one example. If you compile and run below "Bye" will never get printed.

public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Hi");
            System.exit(0);
        }
        finally {
            System.out.println("Bye!");
        }
    }
}
like image 45
Jeff Foster Avatar answered Nov 15 '22 18:11

Jeff Foster