Continue executing loop after catching an exception in try/catch

梦想的初衷 提交于 2019-12-01 16:18:36
PermGenError

put your try/catch inside your while loop:

    while (option != 0) {
        final Scanner keyb = new Scanner(System.in);
        System.out.println("");
        try {
            switch (option) {

            }
        } catch (Exception InputMismachException) {
            System.out.println("\nPlease Enter a Valid Number\n");
            option = menuSystem();
        }
    }

Put the try and catch within the while loop. If the code is using nextInt() then you need to skip the invalid input as it will not be consumed in the event of a mismatch.

It would be possible to avoid the exception handling for InputMismatchException by using the hasNextInt() methods of Scanner until a valid input is entered before attempting to consume it:

while (!kb.hasNextInt()) kb.next();

Another way you can do it:

   List<File> directories;
   ...
   for ( File f : directories ) {
        try {
            processFolder(f);
        } catch( Exception e ) {
            SimpleLog.write(e);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!