2012-03-21 69 views
2

可能重复:
Java: Try-Catch-Continue?如何继续尝试catch语句的执行java中

我的信息从文本文件阅读,我想我的代码抛出如果读取的是无效类型,则为异常。但是,我想不出如何让我的程序继续异常被发现

while(input.hasNext()) { 
     try{ 

     type = input.next(); 
     name = input.next(); 
     year = input.nextInt(); 


     } catch(InputMismatchException ex) 
      { 
      //System.out.println("** Error: Invalid input **"); 
      //code to make program continue 

      } 

    } 

回答

3

后,您可以只让线程离开catch块 - 后继续执行代码。如果你想在catch块之后要跳过的代码,你可以使用continue关键字while循环中......

如果你想检索name失败,发生异常后,检索year,那么你将不得不在每个input.next...()声明周围放置一个try ... catch。在抛出异常的位置您不能重新启动执行。

相关问题