Why am i getting an error while instantiating within try with resources clause?

Viewed 4167

Here is the code which is giving the error -

try(StringWriter stringWriter = new StringWriter()) {

IDE is complaining as Unhandled exception from auto-closeable resource:java.io.IOException Is it necessary to pass something in the constructor here ?

4 Answers

In Java, checked exceptions should be either declared (with throws clause) or handled (with catch, finally blocks) by the function in which those exceptions might be thrown. Your code does not do either, which is why you get a compiler error.

As an aside, see this link.

Related