try/catch/finally

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

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button