[英]Why does my Java program exit after an exception is caught?
I am trying to figure out how to continue a code even after an exception is caught. 我试图弄清楚即使捕获到异常后如何继续执行代码。 Imagine that I have a text file filled with numbers. 想象一下,我有一个充满数字的文本文件。 I want my program to read all those numbers. 我希望我的程序读取所有这些数字。 Now, lets say there is a letter mixed in there, is it possible for the exception to be caught, and then the code continues the loop? 现在,假设其中混有一个字母,是否有可能捕获到异常,然后代码继续循环? Would I need the Try and catches within a do-while loop? 我是否需要在do-while循环中进行Try和catch? Please provide me with your ideas, I'd greatly appreciate it. 请向我提供您的想法,我们将不胜感激。 I have provided my code just in case: 我提供了我的代码以防万一:
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;
}
Declare your try-catch block inside your loop, so that loop can continue in case of exception. 在循环中声明try-catch块,以便在异常情况下该循环可以继续进行。
In your code, Scanner.nextDouble
will throw InputMismatchException
if the next token cannot be translated into a valid double value. 在您的代码中,如果下一个标记不能转换为有效的double值,则Scanner.nextDouble
将引发InputMismatchException
。 That is that exception you would want to catch inside your loop. 那就是您想在循环中捕获的异常。
是的,尽管我认为您需要删除return语句,但是我会将try / catch放入while循环中。
Yep. 是的。 These guys have it right. 这些家伙说对了。 If you put your try-catch inside the loop, the exception will stay "inside" the loop. 如果将try-catch放入循环中,则异常将保留在循环“内部”。 But the way you have it now, when an exception is thrown, the exception will "break out" of the loop and keep going until it reaches the try/catch block. 但是,现在的处理方式是,当引发异常时,该异常将“跳出”循环并继续进行,直到到达try / catch块为止。 Like so: 像这样:
try while
^
|
while vs try
^ ^
| |
Exception thrown Exception thrown
In your case you want two try/catch blocks: one for opening the file (outside the loop), and another for reading the file (inside the loop). 在您的情况下,您需要两个 try / catch块:一个用于打开文件(在循环外),另一个用于读取文件(在循环内)。
If you want continue after catching exception: 如果要在捕获异常后继续:
Remove return statement when you encounter exception. 遇到异常时删除return语句。
Catch all possible exceptions inside and outside while loop since your current catch block catches only 2 exceptions. 在您的while循环内部和外部捕获所有可能的异常,因为您当前的catch块仅捕获2个异常。 Have a look at possible exceptions with Scanner API. 查看Scanner API可能出现的异常。
If you want to continue after any type of exception, catch one more generic Exception . 如果要在任何类型的异常之后继续执行,请捕获另一个通用Exception。 If you want to exit in case of generic Exception, you can put return by catching it. 如果要在通用Exception的情况下退出,则可以通过捕获它来返回return。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.