Does a break leave just the try/catch or the surrounding loop?

后端 未结 6 749
花落未央
花落未央 2020-12-29 01:28

If I have a try ... catch block inside a while loop, and there#s a break inside the catch, does program execution leave t

6条回答
  •  孤城傲影
    2020-12-29 02:19

    A break statement always applies to the innermost while, do, or switch, regardless of other intervening statements. However, there is one case where the break will not cause the loop to exit:

    while (!finished) {
        try {
            doStuff();
        } catch (Exception e) {
            break;
        } finally {
            continue;
        }
    }
    

    Here, the abrupt completion of the finally is the cause of the abrupt completion of the try, and the abrupt completion of the catch is lost.

提交回复
热议问题