zoukankan      html  css  js  c++  java
  • In Java, will the code in the finally block be called and run after a return statement is executed?

     

    The answer to this question is a simple yes – the code in a finally block will take precedence over the return statement. Take a look at the code below to confirm this fact:

    Code that shows finally runs after return

    class SomeClass
    {
        public static void main(String args[]) 
        { 
            // call the proveIt method and print the return value
        	System.out.println(SomeClass.proveIt()); 
        }
    
        public static int proveIt()
        {
        	try {  
                	return 1;  
        	}  
        	finally {  
        	    System.out.println("finally block is run 
                before method returns.");
        	}
        }
    }
    

      

    Running the code above gives us this output:

    finally block is run before method returns.
    1
    

      

    From the output above, you can see that the finally block is executed before control is returned to the “System.out.println(SomeClass.proveIt());” statement – which is why the “1” is output after the “finally block is run before method returns.” text.

     

    Very unique situations when finally will not run after return

    The finally block will not be called after return in a couple of unique scenarios: if System.exit() is called first, or if the JVM crashes.

    What if there is a return statement in the finally block as well?

    If you have a return statement in both the finally block and the try block, then you could be in for a surprise. Anything that is returned in the finally block will actuallyoverride any exception or returned value that is inside the try/catch block. Here is an example that will help clarify what we are talking about:

    public static int getANumber(){
        try{
            return 7;
        } finally {
            return 43;
        }
    }
    

      

    The code above will actually return the “43” instead of the “7”, because the return value in the finally block (“43″) will override the return value in the try block (“7″).

    Also, if the finally block returns a value, it will override any exception thrown in the try/catch block. Here is an example:

    public static int getANumber(){
        try{
            throw new NoSuchFieldException();
        } finally {
            return 43;
        }
    }
    

      

    A return statement in the finally block is a bad idea

    Running the method above will return a “43” and the exception in the try block will not be thrown. This is why it is considered to be a very bad idea to have a return statement inside the finally block.

  • 相关阅读:
    BZOJ 2456 mode
    BZOJ 1041 [HAOI2008]圆上的整点
    东北育才 第6天和第7天
    POJ 3692 Kindergarten(最大团问题)
    KM算法及其应用
    UVA 11582 Colossal Fibonacci Numbers!(循环节打表+幂取模)
    ZOJ 3960 What Kind of Friends Are You?(读题+思维)
    POJ 2349 Arctic Network(最小生成树中第s大的边)
    HDU 1576 A/B(欧几里德算法延伸)
    NYOJ 1013 除法表达式(欧几里德算法+唯一分解定理)
  • 原文地址:https://www.cnblogs.com/hephec/p/4586770.html
Copyright © 2011-2022 走看看