As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center获取指导。




7年前关闭。





我正在阅读一本名为《 Java Puzzlers》的Java书籍,在其中我发现..永远不要退出带有return,break,continue或tHRow的finally块,并且永远不允许经过检查的异常传播到finally块之外。您能否用一些简短的例子详细解释一下,以便我能完全理解。

最佳答案

//运行此代码,您将看到运行此代码时,将获得以下值:

//由finally块返回,因此即使try和

//抓住。

class A
{
int one()
    {
        try
            {
             int n[]= new int[5];
             System.out.println("inside try");
             n[7]=89;
             return 10;
            }
        catch(Exception e)
            {
             System.out.println(e);
             return 399;
            }
        finally
            {
             System.out.println(" this is finally block");
             return 20; //priority is given to finally block
            }
        }
 }

class final7
{
    public static void main(String args[])
    {
    A ob= new A();
    System.out.println(ob.one());
    }
}

09-16 03:15