简体   繁体   English

在Java中,在引发异常的方法中关闭变量的正确方法是什么?

[英]In Java, what is the right way to close variables in a method that throws exceptions?

In Java, I often close variables in a finally block, like so: 在Java中,我经常在finally块中关闭变量,如下所示:

public void someMethod(){
  InputStream inStream = null;
  PreparedStatement pStatement = null;

  try{ do something here }
  catch(Exception ex){ do something here }
  finally{
    try{ if (inStream != null) inStream.close(); } catch(Exception ex){/* do nothing */}
    try{ if (pStatement != null) pStatement.close(); } catch(Exception ex){/* do nothing */}
  }
}

I am wondering, if the method says it throws an exception, is there a place like a "finally" that I can close the method's variables? 我想知道,如果该方法说它引发了异常,是否有一个像“最终”这样的地方可以关闭该方法的变量? For example: 例如:

public void anotherMethod() throws SQLException {
  // This method doesn't need a try/catch because the method throws an exception.
  InputStream inStream = null;
  PreparedStatement pStatement = null;

  // Where can I ensure these variables are closed?
  // I would prefer not to have them be global variables.
}

The right way would actually be to use the try-with-resources construct that was introduced in Java 7. 正确的方法实际上是使用Java 7中引入的try-with-resources构造。

public void anotherMethod() throws SQLException {
    try (PreparedStatement st = connection.prepareStatement(...)) {
        // do things with st
    }
}

This ensures that whatever happens in the try block (executed successfully or ends with an exception), the resources will be closed. 这样可以确保在try块中发生的任何事情(成功执行或以异常结束)都会关闭资源。 You don't need to add a catch part since the method throws the SQLException , and more importantly, you don't need to add a finally clause: all opened resources are guaranteed to be closed after that statement. 您无需添加catch部分,因为该方法会引发SQLException ,更重要的是,您无需添加finally子句:保证所有已打开的资源在该语句之后都将被关闭。

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

相关问题 找出方法以编程方式抛出的异常 - Find out what exceptions a method throws programmatically JAVA:JUnitTest,抛出两个异常的测试方法 - JAVA: JUnitTest, testing method that throws two exceptions 在 Java 8 的链式语句中检查 null 或检查异常的正确方法是什么? - What's the right way to check null or check exceptions in a chained statement in Java 8? Java:编写通用HashMap合并方法的正确方法是什么? - Java: What's the right way of writing a generic HashMap merge method? 当被调用的方法引发大量异常时处理异常的最佳方法 - Best way to handle exceptions when the invoked method throws a lot of exception Java中的异常抛出顺序 - Order of throws exceptions in Java Java中异常的throws关键字 - The throws keyword for exceptions in Java Java8可选替换抛出多个异常的方法 - Java8 Optional to replace method that throws several exceptions Java:有没有正确的方法来使用静态volatile变量? - Java: Is there a right way to use static volatile variables? 为什么java.lang.AutoCloseable的close方法会抛出异常,但java.io.Closeable的close方法会抛出IOException? - Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM