Exception in try with resources clause

2.1k views Asked by At
class Demo
{
    public static void main(String args[]) throws java.io.IOException
    {
        try(FileInputStream fin = new FileInputStream("Demo.txt"))
        {
            //This block is executed successfully
        }
        
        System.out.println("Will it be executed if error occurs in try clause");
    }
}

Suppose that the code in try block is executed successfully as mentioned in the code, and some exception occurs in try with resource clause, that means exception occurs in auto closing of file.

How that exception in the try with resource clause will be handled?

What I want to ask is that will that exception be thrown to JVM and will terminate my program abruptly and the println statement will not be executed?

Can I catch that exception so that remaining program can also be executed?

4

There are 4 answers

0
Safwan Hijazi On BEST ANSWER

only add catch clause, to catch the exception otherwise program will be terminated

public static void main(String[] args) throws FileNotFoundException, IOException {
        try(FileInputStream fin = new FileInputStream("Demo.txt"))
        {
            //This block is executed successfully
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("Will it be executed if error occurs in try clause");
    }
1
K Erlandsson On

If an exception is thrown by the close method of an AutoClosable it will indeed be thrown after the try block has been executed.

If you need to handle the exception you can simply add a catch clause to your try clause.

The following code illustrates the behavior:

public class Foo {

    public static class Bar implements AutoCloseable {

        @Override
        public void close() {
            throw new RuntimeException();
        }

    }

    public static void main(String args[]) {
        try (Bar b = new Bar()) {
            // This block is executed successfully
        }

        System.out.println("Will it be executed if error occurs in try clause");
    }
}

It terminates the JVM with the stack trace:

Exception in thread "main" java.lang.RuntimeException
at test3.Foo$Bar.close(Foo.java:14)
at test3.Foo.main(Foo.java:25)

25 is the line where the closing } for my try clause is.

It could be handled by using:

try (Bar b = new Bar()) {
    // This block is executed successfully
} catch (Exception e) {
     // ...
}
7
Valentin Ruano On

I think with a finally the rest of the program will be run, please try it and report.

class Demo

{
    public static void main(String args[]) throws java.io.IOException
    {
        try(FileInputStream fin = new FileInputStream("Demo.txt"))
        {
            //This block is executed successfully
        } finally {
           System.out.println("Will it be executed if error occurs in try clause");
        }
    }
}
0
mmalik On

Just delete the file Demo.txt and run the following code.

The simplest way to throw such exception is to run this code, with no existing resource (Demo.txt in this case):

public static void main(String args[])
    {
        try(FileInputStream fin = new FileInputStream("Demo.txt"))
        {

        } catch(IOException exc) {
            System.out.println("An exception has occured. Possibly, the file does not exist. " + exc);
        }

        System.out.println("Will it be executed if error occurs in try clause");
    }