尝试使用资源与Try-Catch [英] Try With Resources vs Try-Catch

查看:129
本文介绍了尝试使用资源与Try-Catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看代码,我看过尝试使用资源。我以前使用过标准的try-catch语句,看起来它们做同样的事情。所以我的问题是尝试使用资源和尝试捕获这些之间有什么区别,哪个更好。

I have been looking at code and I have seen try with resources. I have used the standard try-catch statement before and it looks like they do the same thing. So my question is Try With Resources vs Try-Catch what are the differences between those, and which is better.

这是一个尝试资源:

objects jar = new objects("brand");
objects can= new objects("brand");

try (FileOutputStream outStream = new FileOutputStream("people.bin")){
    ObjectOutputStream stream = new ObjectOutputStream(outStream);

    stream.writeObject(jar);
    stream.writeObject(can);

    stream.close();
} catch(FileNotFoundException e) {
    System.out.println("sorry it didn't work out");
} catch(IOException f) {
    System.out.println("sorry it didn't work out");
}


推荐答案

尝试的要点 - with-resources用于确保资源关闭,而无需应用程序代码。但是要考虑一些更好的点。

The main point of try-with-resources is to make sure resources are closed, without requiring the application code to do it. There are some finer points to consider, though.

当你不使用try-with-resources时,可能存在一个称为异常屏蔽的陷阱。当try块中的代码抛出异常,并且finally中的close方法也抛出异常时,try块抛出的异常会丢失,并且finally中抛出的异常会被传播。这通常是不幸的,因为关闭时抛出的异常是无用的,而有用的异常是信息性异常。使用try-with-resources关闭资源可以防止发生任何异常屏蔽。

When you don't use try-with-resources there's a potential pitfall called exception-masking. When code in a try block throws an exception, and the close method in the finally also throws an exception, the exception thrown by the try block gets lost and the exception thrown in the finally gets propagated. This is usually unfortunate, since the exception thrown on close is something unhelpful while the useful exception is the informative one. Using try-with-resources to close your resources will prevent any exception-masking from taking place.

作为确保异常屏蔽不会丢失重要异常信息的一部分当开发try-with-resources时,他们必须决定如何处理close方法抛出的异常。

As part of making sure exception-masking wouldn't lose important exception information, when try-with-resources was developed they had to decide what to do with the exceptions thrown from the close method.

使用try-with-resources,如果尝试block抛出异常,close方法也抛出异常,然后关闭块的异常被添加到原始异常

With try-with-resources, if the try block throws an exception and the close method also throws an exception, then the exception from the close block gets tacked on to the original exception:


..在某些情况下,可以在兄弟代码块中抛出两个独立的异常,特别是在try-with-resources语句的try块和编译器生成的finally块中,它关闭资源。在这些情况下,只能传播一个抛出的异常。在try-with-resources语句中,当存在两个此类异常时,将传播源自try块的异常,并将finally块中的异常添加到由try块中的异常抑制的异常列表中。作为异常展开堆栈,它可以累积多个抑制异常。

... there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.

另一方面,如果您的代码正常完成但您正在使用的资源在关闭时抛出异常,该异常(这将是如果try块中的代码抛出任何内容,则会被抛出。这意味着,如果您有一些JDBC代码,其中ResultSet或PreparedStatement由try-with-resources关闭,则可以抛出JDBC对象关闭时由于某些基础结构故障导致的异常,并且可以回滚否则将成功完成的操作。

On the other hand if your code completes normally but the resource you're using throws an exception on close, that exception (which would get suppressed if the code in the try block threw anything) gets thrown. That means that if you have some JDBC code where a ResultSet or PreparedStatement is closed by try-with-resources, an exception resulting from some infrastructure glitch when a JDBC object gets closed can be thrown and can rollback an operation that otherwise would have completed successfully.

没有try-with-resources是否抛出close方法异常取决于应用程序代码。如果在try块抛出异常时它被抛出finally块,则finally块中的异常将掩盖另一个异常。但是开发人员可以选择捕获关闭时抛出的异常,而不是传播它。

Without try-with-resources whether the close method exception gets thrown is up to the application code. If it gets thrown in a finally block when the try block throws an exception, the exception from the finally block will mask the other exception. But the developer has the option of catching the exception thrown on close and not propagating it.

这篇关于尝试使用资源与Try-Catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆