Java 是否有防止自动异常处理的方法?

Java 是否有防止自动异常处理的方法?,java,exception,try-catch,Java,Exception,Try Catch,如果用户输入一个非数值,例如“Hello”,我想给用户一个新的机会输入一个数值,直到他成功为止 运行下面的代码,如果用户输入非数值,控制台中将显示文本“这不是数值。输入数值:”。但是,用户将无法输入新值。因为错误消息“线程中出现异常…”将显示并停止程序 我如何解决这个问题 编辑 新代码(此代码有效,但它会完全停止程序。之后调用的每个方法都不会运行。)Edit2它不起作用 int number; do { try { System.out.println("Enter a

如果用户输入一个非数值,例如“Hello”,我想给用户一个新的机会输入一个数值,直到他成功为止

运行下面的代码,如果用户输入非数值,控制台中将显示文本“这不是数值。输入数值:”。但是,用户将无法输入新值。因为错误消息“线程中出现异常…”将显示并停止程序

我如何解决这个问题

编辑
新代码(此代码有效,但它会完全停止程序。之后调用的每个方法都不会运行。)Edit2它不起作用

int number;

do {
  try {
    System.out.println("Enter a numeric value: ");
    number = Integer.parseInt(s.nextLine());

    if (s.nextInt() != (int)number) {
      throw new NumberFormatException();
    }
    break;
  }
  catch (NumberFormatException e) {
    System.out.println("This is not a numeric value. Enter a numeric value: ");
  }
} while (true)

旧代码

int number;

try {
  Scanner s = new Scanner(System.in);

  System.out.println("Enter a numeric value: ");
  number = s.nextInt();

  if (number != (int)number) {
    throw new Exception();
  }
}
catch(Exception e) {
  do {
    System.out.println("This is not a numeric value. Enter a numeric value: ");
    number = s.nextInt();
  } while (number != (int)number);
}

请尝试以下给定代码:

int number;
Scanner s = new Scanner (System.in);
  System.out.println ("Enter a numeric value: ");

do
  {
try
{

  number = Integer.parseInt (s.nextLine ());
  //Do your work
  break;
}
catch (Exception e)
{
  System.out.println
    ("This is not a numeric value. Enter a numeric value: ");

}
  }
while (true);

}

使用NumberFormatException而不是基本异常类

int number;
Scanner s = new Scanner(System.in);
System.out.println("Enter a numeric value: ");

do {
    try {

        number = Integer.parseInt(s.nextLine());
        //Do your work
        break;
    } catch (NumberFormatException e) {
        System.out.println("This is not a numeric value. Enter a numeric value: ");

    }
}
while (true);

}

听起来你想一直循环直到用户输入一个有效的整数。表示“永远循环”的常用方法是使用
而(true){…}
。这是一个“无限循环”。退出此循环的唯一方法是使用
break
语句或
return
语句(或者通过抛出异常,但在这种情况下,您希望捕获异常并继续循环)

使用
scanner.nextInt()
的一个问题是,如果该值不是整数,则该值将保留在扫描仪的缓冲区中。如果您继续调用
nextInt()
,您只会不断得到
inputmaschException
s,因为扫描仪会一次又一次地尝试将相同的错误输入解释为整数

解决此问题的一种方法是使用
scanner.nextLine()
将值读取为字符串,然后使用
Integer.parseInt(String)
字符串
转换为
int
。如果转换失败,
Integer.parseInt(String)
抛出一个
NumberFormatException
。您可以通过捕获来处理此异常

这里有一个小函数,它会一直循环,直到用户输入一个可以解析为
int
的值:

public static int promptForInt(Scanner scanner) {
  while (true) {
    System.out.println("Enter a numeric value:");
    try {
      return Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException e) {
      System.out.print("This is not a numeric value. ");    
    }
  }
}
您可以这样调用该方法:

Scanner scanner = new Scanner(System.in);
int number = promptForInt(scanner);

尝试以下方法,如果将输入作为字符串,则可以解析int以检查其是否为整数值

    String number = null;

    Scanner s = new Scanner(System.in);

    System.out.println("Enter a numeric value: ");
    number = s.next();

    try {
        // checking valid integer using parseInt() method
        Integer.parseInt(number);

    } catch (NumberFormatException e) {
        System.out.println("This is not a numeric value. Enter a numeric value:");
        number = s.next();

        // put this line here to prove it accepted the second attempt
        System.out.println("Your entered integer is:" + number);
    }
    s.close();

}

}

我认为您并不真正理解异常的来源。行
抛出新异常()从不在代码中运行。这是因为您的if语句说的是
if(number!=(int)number)
,这永远不会是真的,因为变量
number
已经是
int
,所以您基本上说的是
if(number!=number)
,这永远不会是真的。如果查看Scanner的
nextInt()
方法,您会注意到它实际上抛出了
inputmaschException
。这是您使用try-catch捕获的异常,但不是导致错误消息的异常。那么这个例外是从哪里来的呢?行
number=s.nextInt()是不可编译的代码。您在try块中定义了
s
,因此
s
仅存在于try块中。当您试图在catch块中访问它时,您会得到一个异常,因为
s
在那里不是一个变量;就编译器而言,它并不存在。解决方法非常简单;只需将
s
的声明移动到try块的外部,并在try-catch块周围放置while循环。还要记住,每次尝试读取int时,都要使用
s.nextLine()
来使用输入流中的行分隔符。下面是一个示例:

int number;
Scanner s = new Scanner(System.in); //declare s outside of the try block

System.out.println("Enter a numeric value: ");

while (true) { //while loop goes around the try-catch block
  try {
    number = s.nextInt(); //this could throw InputMismatchException
    break; //if no exception is thrown, break out of the loop
  } catch (InputMismatchException e) { //if the exception is thrown
    s.nextLine(); //consume the line separator character(s)
    System.out.println("This is not a numeric value. Enter a numeric value: "); //prompt the user for another value
    //this will then go back to the top of the try block again, because we never broke out of the while loop
  }
}

s.nextLine(); //consume the line separator character(s)

你有一个做,而在你的捕捉块循环。为了在捕获非数字输入的同时不断提示用户输入,您还可以将该循环移动到其他什么位置?检查
number!=(int)number
没有意义,因为
number
是一个
int
。如果你的输入不是一个int,你就不会到达那一行。这回答了你的问题吗?这确实解决了我显示的问题。但是,它完全停止了程序。之后调用的任何方法都不会运行。你有办法解决这个问题吗?