java异常处理和延续

时间:2012-08-20 21:07:00

标签: java

我有一个Java程序,我可以从不同的源获取数据。有时在阅读时我看到Exception并且程序正在退出。 我的节目是每隔10分钟运行一次。

Public static void main(Strings[] args)
{
...readsource();
}

Private static void readsource() throws IOException
{
...
}

问题: 我能够得到/看到例外。但我希望该计划能够继续下去 那个最好的逻辑是什么?我没有看到try-catch-finally也没有解决..我希望程序在查看异常之后继续(我的意思是下一次迭代应该继续)。这看起来是一个基本问题,不知道如何解决这个问题......

4 个答案:

答案 0 :(得分:2)

然后你需要捕获你目前没有做的异常。

try  {
    readsource();
} catch (IOException e) {
   // do something, never catch an exception and not do anything
}

//continue.

请注意,例外通常表示出现了问题。除非您要对异常做些什么,否则最好修复导致异常的条件......

答案 1 :(得分:1)

您必须在方法中提供错误处理程序,即使用try-catch块围绕对readsource()的调用。

   public static void main(Strings[] args)
   {
      try{
         ...readsource();
      }
      catch(IOException ioe){
           //handle the error here,e.g don't do anything or simply log it
      }
    }

答案 2 :(得分:1)

如果不在catch块中重新抛出异常,执行将从catch块的末尾开始,并继续执行,就像没有异常一样。

答案 3 :(得分:1)

如果你的意思是你想要回忆一下这个方法,那就抛出异常,或者只是把它放在一个while循环中,即:

Public static void main(Strings[] args)
{
    boolean run=true;
    while(run) {
    try {
            System.out.print("Hello,");
            readsource();
            throw new IOException();
            if(1==2)run=false;//stop the loop for whatever condition
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        System.out.println(" world!");
    }
   }

}

Private static void readsource() throws IOException
{
...
}