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

查看:28
本文介绍了尝试使用资源 vs 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");
}

推荐答案

try-with-resources 的主要目的是确保资源可靠关闭,而不会丢失信息.

The main point of try-with-resources is to make sure resources are closed reliably without possibly losing information.

当您不使用 try-with-resources 时,存在称为异常屏蔽的潜在陷阱.当 try 块中的代码抛出异常,并且 finally 中的 close 方法也抛出异常时,try 块中抛出的异常丢失,而 finally 中抛出的异常被传播.这通常是不幸的,因为关闭时抛出的异常是无用的,而有用的异常是提供信息的异常.(因此,您没有看到告诉您违反了哪个参照完整性约束的 SQLException,而是显示了类似 BrokenPipeException 的内容,其中关闭资源失败.)

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. (So instead of seeing the SQLException that tells you which referential integrity constraint was violated, you're shown something like BrokenPipeException where closing the resource failed.)

这种异常屏蔽是一个恼人的问题,try-with-resources 可以防止发生.

This exception-masking is an annoying problem that try-with-resources prevents from happening.

为了确保异常屏蔽不会丢失重要的异常信息,在开发 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,如果try块抛出异常,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.

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

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