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

前端 未结 9 1996
没有蜡笔的小新
没有蜡笔的小新 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:16

    You can make it a part of Daemon Thread. You may use the method setDaemon(boolean status) which is used to mark the current thread as daemon thread or user thread and exit the JVM as and when required. This will enable you exit the JVM before finally{} is executed.

    0 讨论(0)
  • 2020-11-30 02:17

    I think when JVM exits suddenly due to any reason, that can be a cause the control will not enter into the the finally block and never execute.

    0 讨论(0)
  • 2020-11-30 02:20

    There is a chance of partial execution when finally itself throws an exception (or leads to an error)

    0 讨论(0)
  • 2020-11-30 02:20

    Another possible instance of a finally block never executing would be due to a design where the method returned before the try block was entered, as in the cases of some very bad code I've seen from time to time:

    public ObjectOfSomeType getMeAnObjectOfSomeType() throws SomeHorrendousException {
        if (checkSomeObjectState()) {
            return new ObjectOfSomeType();
        }
    
        try {
            // yada yada yada...
        } catch (SomeHorrendousException shexc) {
            // wow, do something about this horrendous exception...
        } finally {
            // do some really important cleanup and state invalidation stuff...
        }
    

    I know none of you would ever do this, so I hesitated to add this as a possible scenario, but thought, eh, it's Friday, what the heck ; )

    0 讨论(0)
  • 2020-11-30 02:28

    One could be "A finally is a part of daeomon thread it may not be executed".

    0 讨论(0)
  • 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);
    0 讨论(0)
提交回复
热议问题