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

Solution 1:

Yes, it will. Easiest way to find out is to try it.

public static void main(String[] args) {
        int i=0;
        while (i<10) {
            System.out.println(i);
            try {
                if(i ==7){
                    throw new Exception();
                }
                i++;
            } catch (Exception e) {
                break;
            }
        }
        System.out.println("out of loop");
    }

It will print

0
1
2
3
4
5
6
7
out of loop

The output starts with 0.

Solution 2:

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.

Solution 3:

Yes, it'll break the loop.

But why not do:

finished = true;

instead?