简体   繁体   English

try with resources子句中的异常

[英]Exception in try with resources clause

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. 假设try block中的代码如代码中所述成功执行,并且try with resource clause发生了一些exception ,这意味着在auto closing文件时会发生异常。

How that exception in the try with resource clause will be handled? 如何处理try with resource clause中的异常?

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? 我想问的是,该异常会被抛到JVM并且会突然终止我的程序并且不会执行println语句吗?

Can I catch that exception so that remaining program can also be executed? 我可以捕获该异常,以便还可以执行剩余的程序吗?

If an exception is thrown by the close method of an AutoClosable it will indeed be thrown after the try block has been executed. 如果AutoClosableclose方法抛出异常,则在执行try块后确实会抛出异常。

If you need to handle the exception you can simply add a catch clause to your try clause. 如果需要处理异常,只需在try子句中添加一个catch子句即可。

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: 它使用堆栈跟踪终止JVM:

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. 25是在收线}try子句。

It could be handled by using: 可以使用以下方法处理:

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

only add catch clause, to catch the exception otherwise program will be terminated 只添加catch子句,捕获异常否则程序将被终止

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");
    }

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");
        }
    }
}

Just delete the file Demo.txt and run the following code. 只需删除文件Demo.txt并运行以下代码即可。

The simplest way to throw such exception is to run this code, with no existing resource (Demo.txt in this case): 抛出此类异常的最简单方法是运行此代码,没有现有资源(在本例中为Demo.txt):

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");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM