gpt4 book ai didi

java - try/catch 捕获异常后继续执行循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:33:39 26 4
gpt4 key购买 nike

一旦在此代码中捕获到异常,menuSystem 方法就会运行,但是一旦我输入数字,程序就会关闭并显示“构建成功”消息。一旦发生异常,有没有办法回到 while 循环?

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

最佳答案

将您的 try/catch 放入您的 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();
}
}

关于java - try/catch 捕获异常后继续执行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com