Exception is swallowed by finally

前端 未结 7 1885
攒了一身酷
攒了一身酷 2020-12-08 23:54
static int retIntExc() throws Exception{
    int result = 1;
    try {
        result = 2;
        throw new IOException(\"Exception rised.\");
    } catch (ArrayInd         


        
相关标签:
7条回答
  • 2020-12-09 00:44

    This is why you can't return from a finally block in C# :)

    It's absolutely the behaviour laid out in the Java Language Specification though. It's specified in section 14.20.2.

    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).

    Returning is one example of completing abruptly; if the finally block threw an exception, that would also complete abruptly, losing the original exception.

    The quote above was from this nested set of bullet points, leaving out the options which aren't applicable here:

    • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
      • If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
        • 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).
    0 讨论(0)
提交回复
热议问题