如何在获得异常后继续执行线程

时间:2012-03-10 00:49:42

标签: java exception

class TestA implements Runnable {
  public void run() {
    try {
      // do stuff
    } catch(Exception e) {
      // ...
    } finally {
      // ...
    }
  }
}

发生异常时,控件退出程序。我在for循环中有10个文件需要处理。

当第二个文件中存在异常时,其余8个文件不会被处理。但我希望为失败创建一个日志,并继续处理剩余的文件而不终止。有没有办法做到这一点?感谢!!!

1 个答案:

答案 0 :(得分:3)

try / catch逻辑嵌入for循环中:

for(...)
{
    try
    {
        ... // process the file
    }
    catch(...)
    {
        ... // deal with the exception
    }
}
相关问题