为什么我的 Java 程序在捕获到异常后退出?
Why does my Java program exit after an exception is caught?
我正在尝试弄清楚如何在捕获到异常后继续执行代码。想象一下,我有一个充满数字的文本文件。我希望我的程序读取所有这些数字。现在,假设其中混入了一个字母,是否有可能捕获到异常,然后代码继续循环?我需要在 do-while 循环中使用 Try 和 catch 吗?请提供您的想法,我将不胜感激。我提供了我的代码以防万一:
NewClass newInput = new NewClass();
infile2 = new File("GironEvent.dat");
try(Scanner fin = new Scanner (infile2)){
/** defines new variable linked to .dat file */
while(fin.hasNext())
{
/** inputs first string in line of file to variable inType */
inType2 = fin.next().charAt(0);
/** inputs first int in line of file to variable inAmount */
inAmount2 = fin.nextDouble();
/** calls instance method with two parameters */
newInput.donations(inType2, inAmount2);
/** count ticket increases */
count+=1;
}
fin.close();
}
catch (IllegalArgumentException ex) {
/** prints out error if exception is caught*/
System.out.println("Just caught an illegal argument exception. ");
return;
}
catch (FileNotFoundException e){
/** Outputs error if file cannot be opened. */
System.out.println("Failed to open file " + infile2 );
return;
}
在循环中声明 try-catch 块,以便在出现异常时循环可以继续。
在您的代码中,如果无法将下一个标记转换为有效的双精度值,Scanner.nextDouble
将抛出 InputMismatchException
。那就是您希望在循环中捕获的异常。
是的,我会把你的 try/catch 放在你的 while 循环中,尽管我认为你需要删除你的 return 语句。
是的。这些人是对的。如果将 try-catch 放在循环中,异常将留在 "inside" 循环中。但是你现在的方式是,当抛出异常时,异常将 "break out" 循环并继续进行直到它到达 try/catch 块。像这样:
try while
^
|
while vs try
^ ^
| |
Exception thrown Exception thrown
在您的情况下,您需要 两个 try/catch 块:一个用于打开文件(在循环外),另一个用于读取文件(在循环内) .
如果你想在捕获异常后继续:
遇到异常时去掉return语句。
捕获 while 循环内外所有可能的异常,因为您当前的 catch 块仅捕获 2 个异常。查看 Scanner API 可能出现的异常。
如果您想在发生任何类型的异常后继续,请再捕获一个通用异常。如果你想在通用异常的情况下退出,你可以通过捕获它来放置 return 。
我正在尝试弄清楚如何在捕获到异常后继续执行代码。想象一下,我有一个充满数字的文本文件。我希望我的程序读取所有这些数字。现在,假设其中混入了一个字母,是否有可能捕获到异常,然后代码继续循环?我需要在 do-while 循环中使用 Try 和 catch 吗?请提供您的想法,我将不胜感激。我提供了我的代码以防万一:
NewClass newInput = new NewClass();
infile2 = new File("GironEvent.dat");
try(Scanner fin = new Scanner (infile2)){
/** defines new variable linked to .dat file */
while(fin.hasNext())
{
/** inputs first string in line of file to variable inType */
inType2 = fin.next().charAt(0);
/** inputs first int in line of file to variable inAmount */
inAmount2 = fin.nextDouble();
/** calls instance method with two parameters */
newInput.donations(inType2, inAmount2);
/** count ticket increases */
count+=1;
}
fin.close();
}
catch (IllegalArgumentException ex) {
/** prints out error if exception is caught*/
System.out.println("Just caught an illegal argument exception. ");
return;
}
catch (FileNotFoundException e){
/** Outputs error if file cannot be opened. */
System.out.println("Failed to open file " + infile2 );
return;
}
在循环中声明 try-catch 块,以便在出现异常时循环可以继续。
在您的代码中,如果无法将下一个标记转换为有效的双精度值,Scanner.nextDouble
将抛出 InputMismatchException
。那就是您希望在循环中捕获的异常。
是的,我会把你的 try/catch 放在你的 while 循环中,尽管我认为你需要删除你的 return 语句。
是的。这些人是对的。如果将 try-catch 放在循环中,异常将留在 "inside" 循环中。但是你现在的方式是,当抛出异常时,异常将 "break out" 循环并继续进行直到它到达 try/catch 块。像这样:
try while
^
|
while vs try
^ ^
| |
Exception thrown Exception thrown
在您的情况下,您需要 两个 try/catch 块:一个用于打开文件(在循环外),另一个用于读取文件(在循环内) .
如果你想在捕获异常后继续:
遇到异常时去掉return语句。
捕获 while 循环内外所有可能的异常,因为您当前的 catch 块仅捕获 2 个异常。查看 Scanner API 可能出现的异常。
如果您想在发生任何类型的异常后继续,请再捕获一个通用异常。如果你想在通用异常的情况下退出,你可以通过捕获它来放置 return 。