try with resource example

The NewResource code below:

public class NewResource implements AutoCloseable{
    String closingMessage;
 
    public NewResource(String closingMessage) {
  this.closingMessage = closingMessage;
    }
 
    public void doSomeWork(String work) throws ExceptionA{
  System.out.println(work);
  throw new ExceptionA("Exception thrown while doing some work");
    }
    public void close() throws ExceptionB{
  System.out.println(closingMessage);
  throw new ExceptionB("Exception thrown while closing");
    }
 
    public void doSomeWork(NewResource res) throws ExceptionA{
  res.doSomeWork("Wow res getting res to do work");
    }
}


Now lets use the NewResource in a sample Program using try-with-resource:

public class TryWithRes {
    public static void main(String[] args) {
  try(NewResource res = new NewResource("Res1 closing")){
res.doSomeWork("Listening to podcast");
  } catch(Exception e){
System.out.println("Exception: "+
 e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
  }
    }
}


Output:

Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA


Nested try-with-resources

public class TryWithRes {
    public static void main(String[] args) {
  try(NewResource res = new NewResource("Res1 closing");
NewResource res2 = new NewResource("Res2 closing")){
try(NewResource nestedRes = new NewResource("Nestedres closing")){
    nestedRes.doSomeWork(res2);
}
  } catch(Exception e){
System.out.println("Exception: "+
e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
  }
 
    }
}


Output:

Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA


Note the order of closing the resources, latest first. Also note that the exception being thrown by each of these close() operations is suppressed.

Lets see how we can retrieve the suppressed exceptions:

Suppressed Exceptions

public class TryWithRes {
    public static void main(String[] args) {
  try(NewResource res = new NewResource("Res1 closing");
NewResource res2 = new NewResource("Res2 closing")){
try(NewResource nestedRes = new NewResource("Nestedres closing")){
    nestedRes.doSomeWork(res2);
}
  } catch(Exception e){
System.out.println("Exception: "+
e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
if (e.getSuppressed() != null){
    for (Throwable t : e.getSuppressed()){
  System.out.println(t.getMessage()+
 " Class: "+t.getClass().getSimpleName());
    }
}
  }
 
    }
}


Output:

Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB

Related Article:

Reference: Java 7 Project Coin: try-with-resources explained with examples from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog

Exit mobile version