Can I avoid such a cumbersome attempt ... catch block

Usually, when it comes to Java IO code, this is what I wrote

FileOutputStream out = null; try { out = new FileOutputStream("myfile.txt"); // More and more code goes here... } catch (Exception e) { } finally { // I put the close code in finally block, to enture the opened // file stream is always closed even there is exception happened. if (out != null) { // Another try catch block, troublesome. try { out.close(); } catch (IOException ex) { } } } 

As you can see, when I try to close the file stream, I need to deal with another try ... catch block.

Look at the concern: (

Is there any way to avoid it? I don’t feel comfortable putting a closed code in a block without ending, because an exception thrown by other codes will not give any chance to call β€œclose”.

+18
java
Jul 22 2018-10-22T00:
source share
7 answers

It is very important that you close the threads at the end. You can simplify this process with a utility, for example:

 public static void closeStream(Closeable closeable) { if(null != closeable) { try { closeable.close(); } catch(IOException ex) { LOG.warning("Failed to properly close closeable.", ex); } } } 

I make this a point, at least by logging a thread failure. Then use will be:

 FileOutputStream out = null; try { out = new FileOutputStream("myfile.txt"); // More and more code goes here... } catch (Exception e) { } finally { closeStream(out); } 

In Java 7, I believe that threads will be automatically closed, and the need for such blocks should be mostly redundant.

+13
Jul 22 '10 at 2:45
source share

Automatic resource management comes with Java 7, which will automatically handle this. Until then, objects such as OutputStream , InputStream and others implement the Closeable interface with Java 5. I suggest you provide a utility method for closing them safely. These methods usually contain exceptions, so be sure to use them only when you want to ignore exceptions (for example, in the finally method). For example:

 public class IOUtils { public static void safeClose(Closeable c) { try { if (c != null) c.close(); } catch (IOException e) { } } } 

Please note that the close() method can be called several times, if it is already closed, subsequent calls will have no effect, therefore they also cause a call to close during the normal operation of the try block, where the exception will not be ignored. From the documentation of Closeable.close :

If the thread is already closed, calling this method has no effect

Close the output stream in a regular code stream, and the safeClose method will only work if something failed in the try block:

 FileOutputStream out = null; try { out = new FileOutputStream("myfile.txt"); //... out.close(); out = null; } finally { IOUtils.safeClose(out); } 
+6
Jul 22 2018-10-22T00:
source share

Discussion

Try-catch-finally, and then try to catch again

and

Is there any preference for nested try / catch blocks?

Basically the question is whether close() should be excluded.

+4
Jul 22 '10 at 2:44
source share

The Lombok project provides the @Cleanup annotation, which eliminates the need to block try catch. Here is an example .

+3
Jul 22 '10 at 4:23
source share

I usually use the utility functions for this:

 public static void safeClose(OutputStream out) { try { out.close(); } catch (Exception e) { // do nothing } } 

which changes the code to a slightly more acceptable one:

 FileOutputStream out = null; try { out = new FileOutputStream("myfile.txt"); // do stuff } catch (Exception e) { // do something } finally { safeClose(out); } 

You really can't do much better in Java, at least until Java 7, when (hopefully) ARM Lock ("Automatic Resource Management") will help a little.

+1
Jul 22 '10 at 2:44
source share

Write a method that looks something like this: a call from your finally block ...

 static void wrappedClose(OutputStream os) { if (os != null) { try { os.close(); } catch (IOException ex) { // perhaps log something here? } } 
+1
Jul 22 2018-10-22T00:
source share

Separate the try / catch and try / finally blocks.

 try { FileOutputStream out = new FileOutputStream("myfile.txt"); try { // More and more code goes here... } finally { out.close(); } } catch (Exception e) { //handle all exceptions } 

An external catch will also catch everything that closes.

0
Jul 22. '10 at 15:03
source share



All Articles