在try / catch中捕获异常后继续执行循环 [英] Continue executing loop after catching an exception in try/catch

查看:2277
本文介绍了在try / catch中捕获异常后继续执行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦在此代码中捕获到异常,就会运行 menuSystem 方法,但是一旦我输入一个数字,程序就会关闭并且Build is successful消息被展示。一旦发生异常,有没有办法重新进入while循环?

Once an exception is caught in this code, the menuSystem method is run, but once I go to input a number the programme closes and the "Build is successful" message is displayed. Is there any way to get back into the while loop once an exception has occured?

public static void main(String[] args) {
   final UnitResults myUnit = new UnitResults(10, "Java");
   int option = menuSystem();

   try {
      while (option != 0) {
         final Scanner keyb = new Scanner(System.in);
         System.out.println("");
         switch (option) {
         }
      }
   } catch (Exception InputMismachException) {
      System.out.println("\nPlease Enter a Valid Number\n");
      option = menuSystem();
   }
}


推荐答案

put while循环中的尝试/抓取

    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();
        }
    }

这篇关于在try / catch中捕获异常后继续执行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆