如何在java中继续执行try catch语句
可能重复:
Java:Try-Catch-Continue?
我正在从文本文件中读取信息,如果读取的类型无效,我希望我的代码抛出exception。 但是,在找到exception后,我无法弄清楚如何继续我的程序
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 } }
您可以让线程离开catch
块 – 之后代码执行将继续。 如果要在catch
块之后跳过代码,可以在while
循环中使用continue
关键字…
如果要在检索name
失败后检索year
后发生exception,则必须在每个input.next...()
语句周围进行try ... catch
。 在抛出exception时,您无法重新启动执行。