Does Java try-with-resources catch errors or just exceptions?

I have some junit tests that create some resources that should also be closed.

One way to implement this logic is to use the @Before and @After .

I did to encapsulate the creation in a utility class for reuse. For example:

 class UserCreatorTestUtil implements AutoClosable { User create() {...} void close() {...} } 

The thing is that the object is closed, instead of forgetting to close it in @After .

Use should be:

 @Test void test() { try (UserCreatorTestUtil userCreatorTestUtil = new UserCreatorTestUtil()) { User user = userCreatorTestUtil.create(); // Do some stuff regarding the user phone Assert.assertEquals("123456789", user.getPhone()); } } 

The problem is that the junit assert keyword throws an Error - not Exception .

Will try-with-resource catch the Error and call the close method?

* Could not find an answer in the try-with-resources documentation.

+49
java exception exception-handling junit
Nov 02 '16 at 11:52
source share
5 answers

This does not mean catch . But it finally closes all resources.

finally blocks are executed even with an error.

+71
Nov 02 '16 at 11:54 on
source share

Pseudo-code of the basic try-with-resources operator (cf Java Language Specification Β§14.20.3.1 ):

 final VariableModifiers_minus_final R Identifier = Expression; Throwable #primaryExc = null; try ResourceSpecification_tail Block catch (Throwable #t) { #primaryExc = #t; throw #t; } finally { if (Identifier != null) { if (#primaryExc != null) { try { Identifier.close(); } catch (Throwable #suppressedExc) { #primaryExc.addSuppressed(#suppressedExc); } } else { Identifier.close(); } } } 

As you can see, it catches a Throwable not Exception that Throwable Error , but only to get the main exception , to add as exceptions all exceptions that occurred when the resources were closed.

You may also notice that your resources are closed in the finally block, which means that they will be closed no matter what happens (except in the case of System.exit , of course, since it terminates the current one by starting the Java virtual machine) even if throws Error or any subclass of the Throwable class.

+36
Nov 02 '16 at 12:03
source share

Try-with-resources will not catch anything.

However, you can attach a catch to the end of the try-with-resources block to catch any Throwable types you like:

 try (UserCreatorTestUtil userCreatorTestUtil = new UserCreatorTestUtil()) { // ... Whatever } catch (RuntimeException e) { // Handle e. } catch (Exception | Throwable t) { // Handle t. } 
+12
Nov 02 '16 at 11:59
source share

The idea of try-with-resources is to make sure that resources must be closed.

The problem with regular try-catch-finally is that let your try block throw an exception; Now you usually handle this exception in the finally block.

Now suppose that an exception also occurs in the finally block. In this case, the exception caused by the catch attempt is lost and an exception is thrown that is thrown in the finally block.

 try { // use something that using resource // eg, streams } catch(IOException e) { // handle } finally { stream.close(); //if any exception occurs in the above line, than that exception //will be propagated and the original exception that occurred //in try block is lost. } 

In try-with-resources the close() try-with-resources method will be called automatically, and if close() throws any exception, the rest of the finally will not be reached, and the original exception will be lost.

Compare this to this:

 try (InputStream inputStream= new FileInputStream("C://test.txt")){ // ... use stream } catch(IOException e) { // handle exception } 

in the above code snippet, the close() method is automatically called, and if this close() method also throws any exception, then this exception will be automatically suppressed.

See also: Java Language Specification 14.20.3

+9
Nov 02 '16 at 12:09
source share

A misconception at your end: try-with-resources does not do a catch.

He makes the final one finally , so the type of β€œproblem” does not matter.

For more information, see JLS !

+5
Nov 02 '16 at 11:56
source share



All Articles