JDK7 new feature try-with-resources statement

A try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is used up. The try-with-resources statement ensures that each declared resource will be closed at the end of the statement. Any object that implements the java.lang.AutoCloseable interface and any object that implements the java.io.Closeable interface can be used as a resource.

The following is an example of use

1. Declare two classes that implement the AutoCloseable interface

package jdk7_new_character;

public class AutoCloseableTestClass implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("autoclose AutoCloseableTestClass");
    }

}
package jdk7_new_character;

public class AutoCloseableTestClass2 implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("autoclose AutoCloseableTestClass2");
        
    }

}

2. Test

package jdk7_new_character;

public class Test {

    @org.junit.Test
    public void test() {
        // fail("Not yet implemented");
    }

    @org.junit.Test
    public  void testAutoClose () {
        System.out.println("begin testAutoClose");
        /*
         * try { AutoCloseableTestClass autoCloseableTestClass = new
         * AutoCloseableTestClass(); } catch (Exception e) { } finally {
         *
         * }
         */

        try (AutoCloseableTestClass autoCloseableTestClass = new AutoCloseableTestClass();
             AutoCloseableTestClass2 autoCloseableTestClass2 = new AutoCloseableTestClass2()
        ) {
            System.out.println("try{}");
        } catch (Exception e) {
            e.printStackTrace ();
        }

        System.out.println("over testAutoClose");
    }

}

3. Running results

 

You can see that you can declare multiple resources in a try-with-resources statement. When the code in the code block terminates, whether normally or abnormally, the close method of the object is automatically called in the reverse order of declaration.

Note: The try-with-resources statement can also have catch and finally blocks like a normal try statement. In a try-with-resources statement, any catch and finally blocks are executed after all declared resources have been closed.

 

suppressed exception

The block of code associated with the try-with-resources statement may throw an exception. Exceptions may be thrown in try blocks, and up to two exceptions may be thrown in try-with-resources statements. If an exception is thrown in the try block and one or more exceptions are thrown in the try-with-resources statement, then the exception thrown in the try-with-resources statement is suppressed and ends up in the method The exception thrown is the one thrown in the try block. You can retrieve the suppressed exception via the Throwable.getSuppressed method of the exception thrown by the try block.

Below is the test

1. Repressed Abnormalities

Declare two classes that implement the AutoCloseable interface and throw an exception in the close method

 

package jdk7_new_character;

/**
 * @author 施俊杰
 */
public class AutoCloseableThrowableTestClass implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("close AutoCloseableThrowableTestClass");
        throw new Exception("AutoCloseableThrowableTestClass exception");
    }

}

 

package jdk7_new_character;

/**
 * @author 施俊杰
 */
public class AutoCloseableThrowableTestClass2 implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("close AutoCloseableThrowableTestClass2");
        throw new Exception("AutoCloseableThrowableTestClass2 exception");
    }

}

Create a test class to create the two classes declared above

 

 

package jdk7_new_character;

public class ThrowableTestClass {

    public String testThrowable() throws Exception {

        try (AutoCloseableThrowableTestClass autoCloseableThrowableTestClass = new AutoCloseableThrowableTestClass();
                AutoCloseableThrowableTestClass2 autoCloseableThrowableTestClass2 = new AutoCloseableThrowableTestClass2()) {
            throw new Exception("exception3");
        }

        
    }
    
    public static void main(String[] args) {
        try {
            new ThrowableTestClass().testThrowable();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            
        }
    }

}

 

operation result

can be seen

AutoCloseableThrowableTestClass2 exception
AutoCloseableThrowableTestClass exception

Two exceptions are suppressed, we can retrieve the suppressed exception through the Throwable.getSuppressed method, the sample code is as follows

package jdk7_new_character;

public class ThrowableTestClass {

    public String testThrowable() throws Exception {

        try (AutoCloseableThrowableTestClass autoCloseableThrowableTestClass = new AutoCloseableThrowableTestClass();
                AutoCloseableThrowableTestClass2 autoCloseableThrowableTestClass2 = new AutoCloseableThrowableTestClass2()) {
            throw new Exception("exception3");
        }

        
    }
    
    public static void main(String[] args) {
        try {
            new ThrowableTestClass().testThrowable();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            Throwable[] suppressed = e.getSuppressed();
            for (Throwable throwable : suppressed) {
                System.out.println(throwable.getMessage());
            }
        }
    }

}

operation result

 

 

This article belongs to the original, if reproduced, please indicate the source

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324492733&siteId=291194637