标签break忽视或正常休息治疗 [英] Labeled break ignored or treated as normal break
问题描述
我的目标平台是Android系统。看来我不能让一个标签break执行甚至一度。我在想什么?看来Android的处理的最后一个标签break作为一个正常的休息,否则它会循环几次。
公共类的测试{公共静态最后的字符串标记= Test.class.getSimpleName();公共静态无效labled_breakTest(){INT计数器= 0;重试:
尝试{
计数器+ = 1;
抛出新SQLiteException(); }赶上(SQLiteException E){
Log.i(TAG,抓,计数器+计数器);
如果(计数器< = 3)破重试;
}最后{
Log.i(TAG,终于,计数器+计数器);
如果(计数器< = 3)破重试;
}计数器= 0;loop_break:
而(计数器3;){
计数器+ = 1; Log.i(TAG,循环计数器+计数器);
打破loop_break;
};
}
}
16 03-07:32:07.709:I /测试(18473):捕捉,计数器1
03-07 16:32:07.709:I /测试(18473):最后,计数器1
03-07 16:32:07.713:I /测试(18473):循环,计数器1
您输出是$ C $正确的C您提供。从您的文章,这听起来就好像你希望控制流将转移到该标签。这不是在Java中的情况。
按照 Java文档:
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
解决方案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.
这篇关于标签break忽视或正常休息治疗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!