Java7, AutoClosable, java7autoclosable

Source: Internet
Author: User

Java7, AutoClosable, java7autoclosable

The try-with-resource Syntax of Java 7 aims to improve the efficiency of Java developers so that they do not need to consider the issue of resource release when writing code, most of these "Cleanup" problems are caused by exceptions that are not called.

Preparation
  • Three exceptions:
Class OpenException extends Exception {} class SwingException extends Exception {} class CloseException extends Exception {}
  • OpenDoor class

The constructor throws OpenException, the swing () method throws SwingException, and the close method throws CloseException.

Class OpenDoor implements AutoCloseable {public OpenDoor () throws Exception {System. out. println ("The door is open. "); // throw new OpenException ()}; public void swing () throws Exception {System. out. println ("The door is becoming unhinged. "); // throw new SwingException ();} public void close () throws Exception {System. out. println ("The door is closed. "); // throw new CloseException ();
  }}
  • Executable TryWithResources class
Public class TryWithResources {public static void main (String [] args) throws Exception {try (OpenDoor door = new OpenDoor () {door. swing ();} catch (Exception e) {System. out. println ("Is there a draft? "+ E. getClass (); // note e. getClass ()} finally {System. out. println (" I'm putting a sweater on, regardless ."
);    }  }}

Run the above Code. If no exception is thrown, the output should be as follows:

The door is open. The door is becoming unhinged. The door is closed. I'm putting a sweater on, regardless.

Three exceptions have been commented out. Now let's throw them one by one.

Start
  • Exception in 'try-with-resource' code block initialization (thrown by the constructor)

What will happen if the OpenDoor constructor throws an exception? Will the close () method be automatically called? Let's give it a try and remove the comments of the Exception Code in the constructor:

Public OpenDoor () throws Exception {System. out. println ("The door is open."); throw new
 OpenException();}

The output of the Code is as follows:

The door is open. Is there a draft? Class OpenException I'm putting a sweater on, regardless.

As you can see, when the constructor throws an exception, the code in the try-with-resource code body is not executed.When declaring a resource, if an exception is thrown, you can think that the resource is not properly initialized, so you need to release the resource. However, if other resources have been correctly initialized, the close () method of those resources will still be called in the reverse order of declaration.

  • Exception thrown in try-with-resource code block

What happens if an exception is thrown in the swing method?

The door is open. The door is becoming unhinged. The door is closed. Is there a draft? Class SwingException I'm putting a sweater on, regardless.

From the above output, we can understand:

It's just the standard behavior of the try-with-resources code block, although confusing: When will the close method be executed? The rule is that the close method of any AutoCloseable object will be executed before any catch Block Code.

  • An exception is thrown in the close method of the AutoCloseable object.
The door is open.The door is becoming unhinged.The door is closed.Is there a draft? class CloseExceptionI'm putting a sweater on, regardless.
I am the dividing line of tiantiao

 

 

Reference: http://www.4byte.cn/learning/84919/java-7-xin-te-xing-zi-dong-zi-yuan-guan-li-arm-he-autoclosable-jie-kou-jiao-cheng.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.