NameSpace
NameSpace

Reputation: 10177

Labeled break ignored or treated as normal break

My target platform is Android. Seems I can't make a labeled break execute even once. What am I missing? It appears Android treated the last labeled break as a normal break, else it would have looped several times.

public class Test{

public static final String TAG = Test.class.getSimpleName();

public static void labled_breakTest(){

int counter = 0; 

retry:
    try{
        counter += 1;
        throw new SQLiteException();

    }catch(SQLiteException e){
        Log.i(TAG, "Catch, Counter " + counter);
        if(counter <= 3) break retry;
    }finally{
        Log.i(TAG, "finally, Counter " + counter);
        if(counter <= 3) break retry;
    }

counter = 0;

loop_break:     
    while(counter < 3){
        counter += 1;

        Log.i(TAG, "Loop, Counter " + counter);
        break loop_break;
    };


}

}

 03-07 16:32:07.709: I/Test(18473): Catch, Counter 1
 03-07 16:32:07.709: I/Test(18473): finally, Counter 1
 03-07 16:32:07.713: I/Test(18473): Loop, Counter 1

Upvotes: 1

Views: 98

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Your output is correct for the code you've provided. From your post, it sounds as though you're expecting control flow to be transferred to the label. This is not the case in Java.

Per the Java docs:

The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

Upvotes: 1

Related Questions