出于某种未知原因,当用户输入为字符串或特殊字符时,它不会捕获我的异常(扫描仪只接受整数和从1到3)。代码如下:
package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CreateUsers {
public static void main(String[] args) {
Scanner chooseNumber = new Scanner(System.in);
int number;
System.out.println("Welcome to UniStudents Application!\n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor \n"
+ "3. Log in as a Secretary \n"
+ "To choose an action, please write the number of the action: ");
number = chooseNumber.nextInt();
while(true) {
if(number > 3 || number < 1) {
System.out.println("The number of the action does not exist.\n"
+"Please try again: ");
chooseNumber.nextInt();
}
else {
System.out.println("ok");
chooseNumber.close();
break;
}
}
try{
chooseNumber.nextInt();
}
catch(InputMismatchException e) {
System.out.println("Please write the number of the action you want to choose \n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor\n"
+ "3. Log in as a Secretary\n");
}
代码的结果是:
1. Log in as a Student
2. Log in as a Professor
3. Log in as a Secretary
To choose an action, please write the number of the action:
$^&*
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at JavaBasics/mainpackage.CreateUsers.main(CreateUsers.java:18)
我搞不懂
因为这个异常是在第18行抛出的,这是number=chooseNumber。nextInt()
以上
而(true)
,并且它不包含在try-catch块中,请尝试:
package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CreateUsers {
public static void main(String[] args) {
Scanner chooseNumber = new Scanner(System.in);
int number;
System.out.println("Welcome to UniStudents Application!\n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor \n"
+ "3. Log in as a Secretary \n"
+ "To choose an action, please write the number of the action: ");
try {
number = chooseNumber.nextInt();
while(true) {
if(number > 3 || number < 1) {
System.out.println("The number of the action does not exist.\n"
+"Please try again: ");
chooseNumber.nextInt();
}
else {
System.out.println("ok");
chooseNumber.close();
break;
}
}
chooseNumber.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please write the number of the action you want to choose \n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor\n"
+ "3. Log in as a Secretary\n");
}
第一次调用number=chooseNumber。nextInt()
不在
try
块中,因此不会捕获异常。将try
块的开头移到它上面,你就没事了。
本节介绍如何使用三个异常处理程序组件(try、catch 和 finally)来编写异常处理程序。 然后,介绍了 Java SE 7中引入的 try-with-resources 语句。 try-with-resources 语句特别适合于使用Closeable的资源(例如流)的情况。 本节的最后一部分将通过一个示例来分析在各种情况下发生的情况。 以下示例定义并实现了一个名为ListOfNumbe
我正在尝试创建一个过滤器来处理异常(请参见在JSF中处理未捕获的异常) 我在日志中看到错误: 我做错了什么?
问题内容: 最近,我遇到了一位程序员的代码,其中他在catch中有一条try-catch语句! 请原谅我无法粘贴实际代码,但是他所做的与以下内容类似: 我个人认为这是我见过的最差的代码!以1到10的比例,您认为我应该多久才能动一下脑子,还是我反应过度? 编辑:他实际上在抓捕中所做的事情,他正在执行一些操作,这些操作在初始尝试失败时可以/应该执行。我的问题是拥有干净的代码和可维护性。将异常从第一个c
请求(正文)
我目前在我的路由中使用dotry/doCatch块,因此我无法使用全局onException块。 然而,如果驼峰路由中断(由于错误代码或意外/未测试的场景),我希望执行一些业务逻辑。希望这永远不会发生,但我仍然想处理更糟糕的情况。 我不能在全局OneException块中有java.lang.Exception,而且,我不想在每个路由上都添加一个额外的捕获。 在抛出未捕获的异常和中断路由之前,是否
问题内容: 我知道可可中有一个UncaughtExceptionHandler,但是我正在为Swift寻找相同的东西。即,每当应用程序中有任何错误/异常由于任何错误而未在本地捕获时,它应该一直冒泡到顶级应用程序对象,在那里我应该能够妥善处理它并适当地响应用户。 Android有它。Flex有它。Java有它。想知道为什么Swift缺少此关键功能。 问题答案: Swift没有机制来捕获所有任意的运行