Source Code : Creating a resource that can be used with the try-with-resources technique

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Creating a resource that can be used with the try-with-resources technique


public class Test {
  public static void main(String[] args) {
    try (MyResource resource1 = new MyResource();
        OtherResource resource2 = new OtherResource()) {
      resource1.do1();
      resource2.do2();
    } catch (Exception e) {
      e.printStackTrace();
      for (Throwable throwable : e.getSuppressed()) {
        System.out.println(throwable);
      }
    }

  }
}

class MyResource implements AutoCloseable {

  @Override
  public void close() throws Exception {
    System.out.println("close method executed");
    throw new UnsupportedOperationException("A problem has occurred");
  }

  public void do1() {
    System.out.println("method executed");
  }
}

class OtherResource implements AutoCloseable {
  @Override
  public void close() throws Exception {
    System.out.println("A");
    throw new UnsupportedOperationException("A problem has occurred");
  }

  public void do2() {
    System.out.println("executed");
  }
}

 

Thank with us