简体   繁体   中英

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? 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.

In your code, Scanner.nextDouble will throw InputMismatchException if the next token cannot be translated into a valid double value. 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. 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. 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).

If you want continue after catching exception:

  1. Remove return statement when you encounter exception.

  2. Catch all possible exceptions inside and outside while loop since your current catch block catches only 2 exceptions. Have a look at possible exceptions with Scanner API.

  3. If you want to continue after any type of exception, catch one more generic Exception . If you want to exit in case of generic Exception, you can put return by catching it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM