Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling IO exceptions in Java

Basically, I want to open a file, read some bytes, and then close the file. This is what I came up with:

try
{
    InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    try
    {
        // ...
        inputStream.read(buffer);
        // ...
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {
        try
        {
            inputStream.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
catch (FileNotFoundException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Maybe I'm spoiled by RAII, but there must be a better way to do this in Java, right?

like image 683
fredoverflow Avatar asked Jun 06 '11 09:06

fredoverflow


People also ask

What is IO exception in Java example?

Java IOExceptions are Input/Output exceptions (I/O), and they occur whenever an input or output operation is failed or interpreted. For example, if you are trying to read in a file that does not exist, Java would throw an I/O exception.

How exceptions are handled in OOP?

In Object-Oriented Programming (OOP), exceptions are a powerful mechanism for centralized processing of errors and exceptional situations. This mechanism replaces the procedure-oriented method of error handling in which each function returns a code indicating an error or a successful execution.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.


3 Answers

If you have the same exception handling code for IOException and FileNotFoundException then you can rewrite your example in a more compact way with only one catch clause:

try {
    InputStream input = new BufferedInputStream(new FileInputStream(file));
    try {
        // ...
        input.read(buffer);
        // ...
    }
    finally {
        input.close();
    }
}
catch (IOException e) {
    e.printStackTrace();
}

You can even get rid of the outer try-catch if you can propagate the exception which probably makes more sense then manually printing the stack trace. If you don't catch some exception in your program you'll get stack trace printed for you automatically.

Also the need to manually close the stream will be addressed in Java 7 with automatic resource management.

With automatic resource management and exception propagation the code reduces to the following:

try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
    // ...
    input.read(buffer);
    // ...
}
like image 190
vitaut Avatar answered Nov 12 '22 11:11

vitaut


Usually these methods are wrapped up in libraries. Unless you want to write at this level, it is best to create your own helper methods or use existing ones like FileUtils.

String fileAsString = Fileutils.readFileToString(filename);
// OR
for(String line: FileUtils.readLines(filename)) {
    // do something with each line.
}
like image 42
Peter Lawrey Avatar answered Nov 12 '22 11:11

Peter Lawrey


I don't know if it is the right way, but you can have all your code in the same try block, and then have the different catch blocks right after each other.

try {
  ...
}
catch (SomeException e) {
  ...
}
catch (OtherException e) {
  ...
}
like image 1
Rasmus Øvlesen Avatar answered Nov 12 '22 11:11

Rasmus Øvlesen