Search code examples
javatry-with-resourcestry-finally

How to call a method in try with resources


I was using try..finally code but I want to use try with resource but I am not sure how to call a method in try with resource can anyone help me with that?

using try finally

try{
}
catch{}
finally{
//closed a resources 
//called a methods 
reportAbc();

}

using Try with resource

try(){
}
catch{}

but I am not sure how should I call reportAbc() method without using finally.


Solution

  • This is from the documentation:

    Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

    https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#:~:text=Note%3A%20A%20try%20%2Dwith%2D,resources%20declared%20have%20been%20closed.