0

Here is my code:

import java.util.Scanner;
import java.io.*;

public class Warning
{
    public static void main (String[] args)throws IOException
    {
        int creditHrs;         
        double qualityPts;     
        double gpa;            
        String name;

        // Set up scanner to input file
        Scanner inFile = new Scanner(new File("c:\\students.dat"));         
        System.out.println ("\n   Students on Academic Warning\n");

        // Process the input file, one token at a time
        try
        {   
            while (inFile.hasNext())
            {
                // Get the credit hours and quality points and
                // determine if the student is on warning. If so,
                // display the student's name.
                name = inFile.next();
                creditHrs = Integer.parseInt(inFile.next());
                qualityPts = Double.parseDouble(inFile.next());

                gpa = qualityPts / creditHrs;
                if(gpa < 2.0)
                {
                    System.out.println(name);
                }
            }
        }

        //insert catch statements
        catch(FileNotFoundException e)
        {
        }
        catch(NumberFormatException e)
        {
        }
        inFile.close();
    }
}

The error is: error: exception FileNotFoundException is never thrown in body of corresponding try statement Why am I getting this? I would think that it not throwing the exception is a good thing and why would it have to tell me that, you know? I really dont understand this.

gotwo
  • 663
  • 8
  • 16
user3587014
  • 19
  • 1
  • 1
  • possible duplicate of [Exception is never thrown in body of corresponding try statement](http://stackoverflow.com/questions/22613423/exception-is-never-thrown-in-body-of-corresponding-try-statement) – andersschuller Apr 29 '14 at 21:22
  • `FileNotFoundException` occurs when you try to open a file and it can't find it. There's nothing in the `try` block that tries to open a file, only code that reads a file. Either you're catching the wrong exception, or there's some sort of file-opening statement up above your `try` block that maybe should be moved down. Or else the `catch(FileNotFoundException e)` block just wandered in from some other program. – ajb Apr 29 '14 at 21:28

2 Answers2

2

You're getting this error because inside of the try statement you're not calling any methods that could even possibly throw this error, as far as the compiler can tell.

Since methods must declare the exceptions they throw, this error is telling you that you're trying to catch an exception that will never happen, which is a coding mistake.

Maybe you had a previous version of this method that might throw this error? If so, maybe this is happening because you've changed the method so it's no longer possible? This is just a guess on my part, but the code sample as you posted it doesn't try to open any new files (that is done before the beginning of the try statement), so it can't happen within the body of the try.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
-1

You are getting this error becuase FileNotFoundExeption will never be thrown by the piece of code inside try block. Try Changing FileNotFoundException to some other exception that will be thrown by the code section inside try block, or just catch Generic excepton (Exception).

Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • Which exception types can be thrown is deterministic - they are declared on method signatures - there's no reason to just try other types, or to catch the general `Exception` class. The catching of `FileNotFound` should wrap the opening of the file earlier in the method if it needs to be caught, or if this method should instead throw that exception to the caller then the catch should just be removed and the exception declared as being thrown in the method signature. – jefflunt Apr 29 '14 at 21:30
  • @jefflunt What I meant was to catch the Exceptions that can be thrown by the piece of code inside try, I am not forcing the OP to handle exceptions just for the sake of it, the choice is the Op's whether the exception needs to be handled inside the function or propagated to the caller. – Kakarot Apr 29 '14 at 21:34