Lewis Briffa
Lewis Briffa

Reputation: 557

The need to catch IOExceptions & Exceptions

Should one always catch an IOException/Exception if a program reading in a txt file using a scanner object produces nothing but a FileNotFoundException?

Would such extra code be unneeded or important?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TestingScanner {
    public TestingScanner() {
        readFile("dummy.txt");
    }

    public void readFile(String filePath) {
        File file = new File(filePath);
        Scanner scanner = null;

        try {
            scanner = new Scanner(file);

            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.matches("^.+@yahoo\\.com?(\\.uk)?$"))
                    System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: Couldn't Load in " + filePath);
            e.printStackTrace();
        } finally {
            if (scanner != null)
                scanner.close();
        }
    }
}

Upvotes: 1

Views: 1624

Answers (3)

Jose Martinez
Jose Martinez

Reputation: 11992

My rule of thumb is to go broad with exception catching (catching Exception or Throwable) unless there is something specific I will want do different for a specific exception.

For example, if a method throws two different Exceptions, and I will be handling both the same then I will just catch Exception. But if I am handling them different then I would catch each individually and process them accordingly.

The wrench in this approach is "what about RuntimeExceptions". One school of thought is to allow RuntimeExceptions to bubble up. But what I find is that in situations where I am catching exceptions, then I want all of them... and sometimes even Throwables (I got burnt once by only catching Exception and not Throwable).

Here are some examples:

public void myMethod() throws IOException, FileNotFoundException ();

In situation where I want to not let Exceptions bubble up (need to deal with them all)

try{
   myMethod();
catch(Exception e) {
//handle it
}

In situation where I am catching Exceptions and need to do something different for FileNotFound.

try{
   myMethod();
catch(FileNotFoundException  fe){
    //handle file not found
}
catch(Exception e) {
    //handle it
}

In situation where I am letting Exceptions bubble up because I know further up the chain some really cool exception handling code is handling it and I want to avoid logging the exception multiple times:

myMethod();

In situation where I am letting Exceptions bubble up except for FileNotFound.

try{
   myMethod();
catch(FileNotFoundException  fe){
    //handle file not found
}

Upvotes: 1

Evald
Evald

Reputation: 122

You can use throws Exception instead, but it is advised to do try/catch since you can tell your program what to do in case of error. And yes, it is necessary to have exceptions in case there is an error.

Upvotes: 1

Ajit
Ajit

Reputation: 92

Yes, we must always catch the IO Exceptions. Since IO is related to reading or writing to a file, there is always a chance that it might fail due to various reasons(wrong input path, unavailability of resource, network failure).

After catching the exception we should always log the exception. In big projects these exception logs helps in identifying the actual cause for some functionality failure.

Upvotes: 0

Related Questions