What are the circumstances under which a finally {} block will NOT execute?

前端 未结 9 2060
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 01:45

In a Java try{} ... catch{} ... finally{} block, code within the finally{} is generally considered \"guaranteed\" to run regardless of what occurs

9条回答
  •  广开言路
    2020-11-30 02:30

    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:

    1. Thread.currentThread().interrupted();
    2. Thread.currentThread().destroy();
    3. Thread.currentThread().stop();
    4. Thread.sleep(10);
    5. Thread.currentThread().interrupt();
    6. Runtime.getRuntime().addShutdownHook(Thread.currentThread());
    7. If there is any exception occurred.
    8. If there is no exception.

    Statements in which finally block is not executed are following:

    1. Thread.currentThread().suspend();
    2. System.exit(0);
    3. JVM crashed.
    4. Power to CPU chip goes off.
    5. OS kills JVM process.
    6. Runtime.getRuntime().exit(0);
    7. Runtime.getRuntime().halt(0);

提交回复
热议问题