Saturday, June 22, 2013

Java 7 - Autocloseable and Suppressed Exception



Summary

- The new interface AutoCloseable becomes the superinterface of Closeable.
- The 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 finished with it.
- The try-with-resources statement ensures that each resource is closed at the end of the statement.
- Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

From: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

See also:
http://www.slideshare.net/alsdias/savedfiles?s_title=new-features-of-jdk-7&user_login=denizoguz



Example

Follow the comments on the code below, a full example to show the suppressed exception.

 


package exception.java7;

import java.io.Closeable;
import java.io.IOException;

/**
 * exception.java7.Autocloseable_main.java<br>
 * project: javaLab<br>
 * <br>
 * target: java 7 AutoCloseable example with suppressed exception. <br>
 */
public class Autocloseable_main {

    public Autocloseable_main() {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //event1();
        event2();
    }

    /**
     * <pre>
     * Event1's output:
     *
     * [INFO]: opening a resource
     * [INFO]: event1(): doing something
     * [INFO]: closing a resource
     * exception.java7.AppException: [WARN]: application failure.
     *     at exception.java7.Autocloseable_main.event1(Autocloseable_main.java:40)
     *     at exception.java7.Autocloseable_main.main(Autocloseable_main.java:31)
     *     Suppressed: exception.java7.AException: [ERROR]: resource failure.
     *         at exception.java7.AResource.close(Autocloseable_main.java:77)
     *         at exception.java7.Autocloseable_main.event1(Autocloseable_main.java:41)
     *         ... 1 more
     *
     * </pre>
     */
    public static void event1() {
        try (AResource resource = new AResource(false);) {
            System.out.println("[INFO]: event1(): doing something");
            if(!resource.success) throw new AppException("[WARN]: application failure.");
        } catch (AException | AppException e) {
            e.printStackTrace();
        }
    }
  
    /**
     * <pre>
     * Event2's output:
     *
     * [INFO]: opening a resource
     * [INFO]: event2(): doing something
     * [INFO]: closing a resource
     * exception.java7.AException: [ERROR]: resource failure.
     *     at exception.java7.AResource.close(Autocloseable_main.java:91)
     *     at exception.java7.Autocloseable_main.event2(Autocloseable_main.java:66)
     *     at exception.java7.Autocloseable_main.main(Autocloseable_main.java:32)
     *
     * </pre>
     */
    public static void event2() {
        try (AResource resource = new AResource(false);) {
            System.out.println("[INFO]: event2(): doing something");
            if(resource.success) throw new AppException("[ERROR]: application failure.");
        } catch (AException | AppException e) {
            e.printStackTrace();
        }
    }
  
}

class AResource implements Closeable {
  
    boolean success = false;
  
    public AResource() {
        System.out.println("[INFO]: opening a resource");
    }

    public AResource(boolean success) {
        System.out.println("[INFO]: opening a resource");
        this.success = success;
    }
  
    @Override
    public void close() throws AException {
        System.out.println("[INFO]: closing a resource");
        boolean notdone = true;
        if (notdone) {
            throw new AException("[ERROR]: resource failure.");
        }
    }
}

class AException extends IOException {

    private static final long serialVersionUID = 1L;
  
    public AException(String msg) {
        super(msg);
    }
}


class AppException extends Exception {

    private static final long serialVersionUID = 1L;

    public AppException(String msg) {
        super(msg);
    }
}

No comments:

Post a Comment

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...