0

I am using Apache PDFBox to read some PDF files. When I am done with the PDF's I need to release the PDF documents. To do this I call the close() method which throws an exception. The program also allows to edit the PDF in which case an exception could be thrown but my program just closes them which shouldn't error. In this case is it acceptable to just catch the exception and leave the catch block empty or is there a better way to deal with it?

For example:

try {
    pdf.close();
catch {
    //empty
}
kabeersvohra
  • 1,049
  • 1
  • 14
  • 31

2 Answers2

2

Is it acceptable to just ignore when a stream close throws exception?

In most cases it is, but it really depends on the context. If you decide not to handle the close exception and are looking to avoid extra try-catch block, then you can use IOUtils from commons-io library.

finally {
    IOUtils.closeQuietly(pdf);
}

This is equivalent to the following

finally {
    try {
        if (closeable != null) {
            closeable.close();
        }
    } catch (IOException ioe) {
        // ignore
    }
}
noscreenname
  • 3,314
  • 22
  • 30
0
    try {
         //code to catch
    } catch (IOException ex) {//specific catch
        ex.printStackTrace();

    } catch (Exception e) {//Catch errors lazily, This will catch any exception within the try that is rooted from the Exception class.
        e.printStackTrace();
    }

There are more proper ways to do this, but this is a easy way. You really only need one catch clause, you can have multi catch's. Google "how to use Try/catch/finally in java" and you should come up with some good stuff.

Good luck!

Bc1151
  • 23
  • 4