我正在为TCPServer编写一个方法。我写了一个代码,代码如下:
// thread run
protected void threadRun(){
// continue running. don't stop
while(true){
try{
try{
}
catch(Exception e1){
try{
} catch(Exception e2){}
finally{
// skip
continue;
}
}
}
catch(Exception e3){
}
}
}内容并不重要。有代码接受客户等,但我已经删除了他们,以确保它不是关于细节。无论如何,当我试图编译这段代码时,编译器说对于continue行:
Error: continue is not inside a loop
考虑到我可能知道错了,我用Java编写了完全相同的代码,如下所示:
class test{
public static void main(String[] args){
while(true){
try{
try{
}
catch(Exception e1){
try{
} catch(Exception e2){}
finally{
continue;
}
}
}
catch(Exception e3){
}
}
}
}正如我所料,java编译器没有给出任何错误消息,编译成功。问题到底是什么呢?
发布于 2013-01-07 04:07:40
显然,continue (和break)不能突破finally块。编译以下代码:
void run() {
loop:
while (true) {
try {}
catch (Exception e) {}
finally {
continue loop;
}
}
}会给你这样的提示(省略标签和你得到的错误是一样的):
Error: cannot continue out of finally block我还没有找到这个限制的理由或解释(编辑:参见下面的ratchet freak的评论)。然而,我不能想象这是一个超级常见的用例。你可能想看看其他的选择。
https://stackoverflow.com/questions/14185219
复制相似问题