Can exception masking be completely prevented with try-with-resources statement

1616 views java
1

I'm aware that try-with-resources statement was introduced to cure (or) prevent exception masking.

Consider the following code:

class TestExceptionSuppressing {
    public static void main(String[] args) {
        try {
            testMethod();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            for (Throwable t : e.getSuppressed()) {
                System.err.println("Suppressed Exceptions List: " + t);
            }
        }
    }

    static void testMethod() {
        try (InnerClass inner = new InnerClass()) {
            throw new IOException("Exception thrown within try block");
        } catch (Exception e) {
            throw new RuntimeException(
                    "Exception thrown within catch block.Hence exception thrown within try block will be lost (or) masked");
        }
    }

    static class InnerClass implements AutoCloseable {

        @Override
        public void close() throws Exception {
            throw new Exception("Exception thrown in close method will be tacked on to existing exceptions");
        }
    }
}

Output: Exception thrown within catch block.Hence exception thrown within try block will be lost (or) masked

Apparently, exception thrown in catch block of testMethod() has masked both the io exception thrown in try block and also the exception suppressed and added to this io exception(thrown in close method )

This code example might prove that try-with-resources may not completely prevent exception masking.

I'm aware things are mixed up in here and might be confused but is a possible case to happen. My question is, is there a way to prevent this scenario i.e. exception masking to still happen even when using try-with-resources statement?

answered question

1 Answer

2

You can keep the original exception if you use RuntimeException(String, Throwable)

Constructs a new runtime exception with the specified detail message and cause.

throw new RuntimeException(
                "Exception thrown within catch block.But try block won't be lost (or) masked", e);

posted this

Have an answer?

JD

Please login first before posting an answer.