How to continue execution of a try catch statement in java [duplicate]

余生颓废 提交于 2020-01-11 10:22:11

问题


Possible Duplicate:
Java: Try-Catch-Continue?

I'm reading in information from a text file and I want my code to throw an exception if there is an invalid type being read. However, I can't figure out how to have my program continue after an exception has been found

    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

          }

    }

回答1:


You can either just let the thread leave the catch block - code execution will continue after that. If you want to skip the code after the catch block, you can use the continue keyword within a while loop...

If you want to retrieve a year after retrieving the name failed with an exception, then you will have to put a try ... catch around each input.next...() statement. You cannot restart execution at the point the exception has been thrown.



来源:https://stackoverflow.com/questions/9813166/how-to-continue-execution-of-a-try-catch-statement-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!