Add throws signature in every method

1.1k Views Asked by At

I'm handling for the first time Exceptions in Java and I want to know if this this the good way to go.

public static void main(String[] args) throws FileNotFoundException {
        submethod();        
    }


    static void submethod() throws FileNotFoundException {
        Scanner scan = new Scanner(new File("file.txt"));

        while (scan.hasNextLine()) {
            // do somethig...
        }
    }

The thing that sounds strange to me is the explicit declaration throws FileNotFoundException also in the main method, otherwise the compiler reports:

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

I wanted to know if I'm doing it wrong. In a more complicated project, where you need to catch much more exceptions, it would become very messy. Is this the better practice to handle exceptions? And why do I need to declare it in both methods?

3

There are 3 best solutions below

1
On BEST ANSWER

Specifying the FileNotFoundException in the throws clause is needed because FileNotFoundException is a checked exception. The other option is to catch the exception and handle it using a try-catch.

It is up to your program requirements to decide on how to handle the exception. For example, if you want to print a certain error message when the exception is thrown, you can catch it:

try {
    Scanner scan = new Scanner(new File("file.txt"));

    while (scan.hasNextLine()) {
        // do somethig...
    }
} catch(FileNotFoundException ex) {
    System.err.println("file.txt was not found");
}

However, in a simple case like the one you have, it would make sense to just terminate the program when the exception occurs. Hence you can just declare the methods to throw the exception as you just did.

In more complex scenarios, you may want to catch the exception, print something to the user, and continue the program. For example, if submethod is used only to read a file provided by the user, then you can keep the throws as it is. However, the caller of the method may want to handle the exception in case it is thrown, and maybe ask the user to re-input another filename.

0
On

You should not handle it like this. You should be catching the error somewhere and handle the exception. You should either catch the error in submethod() or catch it in the main method and handle the exception. The way you have it right now there is no error handling just throwing the error up the stack. Example:

static void submethod() {
    try
    {
        Scanner scan = new Scanner(new File("file.txt"));

         while (scan.hasNextLine()) {
            // do somethig...
         }
    }catch(FileNotFoundException e)
    {
        e.printStackTrace();
        //error handling here
    }
}

Or:

public static void main(String[] args) {
    try
    {
         submethod();
    }catch(FileNotFoundException e)
    {
         e.printStackTrace();
         //error handling code here
    }        
}

In the second case you would throw the FileNotFoundException in your submethod() ie. you would leave the throws declaration in your submethod but not your main method you would handle it there.

0
On

Since your submethod() is not handling FileNotFoundExceptionm exception. The caller of the method need to handle this exception which can be done by enclosing the method call in try catch block or by adding throws to your main method.