try and Finally giving exception with no return statement , but there is no exception when return statement is written in method

一笑奈何 提交于 2019-12-20 07:50:52

问题


Please explain why Exception comes in first program but not in second program.

1) without return statement in read method

class Example
{   
    public static void read()
    {
        try
        {
             int i = 9/0;
        }
        finally
        {
              System.out.println("This proogram is giving exception");
        }       
    }

    public static void main(String[] fel)
    {
         read();
    }
}

2)with return statement in read method

class Example
{   
    public static void read()
    {
         try
         {
               int i = 9/0;
         }
        finally
        {
               System.out.println("This proogram is not giving exception");
               return;
        }       
    }

    public static void main(String[] fel)
    {
          read();
    }
}

回答1:


Branching statements(return ,goto ) should no be used inside finally because execution of such statement nullifies other instructions which are executed before finally.

The Java Language Specification says : If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:

  1. If the finally block completes normally, then the try statement completes abruptly for reason R.
  2. If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

Note - A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded.




回答2:


The reason is given in the Java Language Specification rules governing the execution of try/finally blocks. Essentially

  • if the try block throws an exception E and the finally completes normally then the overall try/finally throws E
  • if the try block throws E but the finally does not complete normally, then E is ignored and the overall try/finally "completes abruptly" for the reason arising from the finally block

An explicit return is considered abrupt rather than normal completion, so in your example 2 the return inside the finally masks the divide-by-zero exception raised by the try.




回答3:


In both instance, the code with throw a java.lang.ArithmeticException, however the return in the finally discards the exception and instead the method exits without forwarding the exception to the caller.

This is one of the reason why in general you should not use a return in a finally-block.

This is described in the Java Language Specification (8), 14.20.2 Execution of try-finally and try-catch-finally:

If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:

... (some similar but for this case irrelevant rules left out)

  • If the run-time type of V is not assignment compatible with a catchable exception class of any catch clause of the try statement, then the finally block is executed. Then there is a choice:

    • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.

    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

You can find the definition of abrupt and normal completion in JLS 14.1 Normal and Abrupt Completion of Statements. But basically a return is considered an abrupt completion.



来源:https://stackoverflow.com/questions/25324170/try-and-finally-giving-exception-with-no-return-statement-but-there-is-no-exce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!