Labled break inside java methods

Why does java allow labled interrupt to be used inside a method? Is there any particular purpose or use of this? I thought this could only be use inside loops and swtches.

public void testMeth(int count){
   label:
   break label;
}

But below is a compiler error.

public void testMeth(int count){
    break; // This gives an Error : break cannot be used outside of a loop or a switch
}
+3
source share
7 answers

I do not know why, but the behavior is specified in the Java language specification # 14.15 :

Split without label

break , while, do ; , , .
switch, while, do for , break, .

( )

break (ยง14.7), , ; , , . , while, do .

( ), . C goto:

C ++ Java goto; (ยง14.15) continue (ยง14.16), .

+2

:

out: { 
         for( int row=0; row< max; row++ ) {
             for( int col=0; col< max; col++ )
                 if( row == limit) break out;
             j += 1;
         }
     }

break , breaking? void, return adarshr.

+2

, , .

0

return !

public void testMeth(int count){
    if(count < 0) {
        return;
    }

    // do something with count
}
0

.

public void testMeth(int count){
    label: if (true) {
          System.out.println("Before break");
          if (count == 2) break label;
          System.out.println("After break");
    }
    System.out.println("After IF");
}

public void testMeth(int count){

    namedBlock: {
        System.out.println("Before break");
        if (count == 0) break namedBlock;
        System.out.println("After break");
    }

    System.out.println("After Block");
}

" ".

0

, :

boolean cond1 = ...
if (cond1) {
    boolean cond1 = ...
    if (cond2) {
        boolean cond3 = ...
        if (cond3) {
            bar();
        } else {
            baz();
        }
    } else {
        baz();
    }
} else {
    baz();
}

... ...

label: {
    boolean cond1 = ...
    if (cond1) {
        boolean cond1 = ...
        if (cond2) {
            boolean cond3 = ...
            if (cond3) {
                bar();
                break label;
            }
        }
    }
    baz();
}

, , . , , -, .

0

labled break. , GOTO. break; / .. : .

. , "Jump-Condition" . , , .

If your method is called getDrink () and it returns a milk object, that's fine. But if your method is called "getWater ()", it should throw an exception instead of returning milk ...

So, instead of:

public class TestBad {

public static void main(String[] args) {
    String[] guys = {"hans", "john"};

    myLabel: {
        for(String guy: guys) {
            String drink = getDrink(guy);

            if(drink.equals("milk")) {
                // Handle "milk"??
                break myLabel;
            }

            // Do something with "non-milk"
        }
    }

    // Success? Non Success??
}

private static String getDrink(String guy) {
    if(guy.equals("hans"))
        return "milk";
    else
        return "water";
}

}

You should use:

public class TestGood {

public static void main(String[] args) {
    String[] guys = {"hans", "john"};

    try {
        handleStuff(guys);
    } catch (Exception e) {
        // Handle Milk here!
    }
}

private static void handleStuff(String[] guys) throws Exception {
    for(String guy: guys) {

        String drink = getWater(guy);

        // Do something with "water"
    }
}

private static String getWater(String guy) throws Exception {
    if(guy.equals("hans"))
        // The method may NEVER return anything else than water, because of its name! So:
        throw new Exception("No Water there!");
    else
        return "water";
}

}

Fazit: Instead of nesting blocks in blocks or multiple loops, you need to nest methods and use the correct exception handling. This improves readability and reuse.

0
source

All Articles