简体   繁体   中英

When is “throws Exception” thrown?

Assume the following function:

public void close() throws SQLException {
    if (rs != null) { rs.close(); }
    if (ps != null) { ps.close(); }
    if (connection != null){ connection.close(); }
}

If I try to close rs and it fails, does the rest of my code still get executed (ie does the exception get thrown at the end), or does it immediatly interrupt the function (and thus prevent ps and connection from getting closed correctly).

If the exception occurs at rs.close , then the exception is thrown. If you want those other statements to be executed, then you need to use finally . If you suspect another exception will be thrown, you can create a function such as closeQuietly which will acknowledge but discard the exceptions if you feel they can be safely ignored. For example (from this answer ),

protected void closeQuietly( Resource resource ) {
      try {
        if (resource != null) {
          resource.close();
        }
      } catch( Exception ex ) {
        log( "Exception during Resource.close()", ex );
      }
    }

When rs.close() throws an exception rest of the lines won't be executed. Caught exception will propagate.

If you want other statement not to interrupt with the others, use separate try/catch block for each closing.

You can create a utility method to close the resources, then call that method with the resources. Since AutoCloseable is super interface of Connection , Statement , ResultSet , you can define a utility method like below

public static void closeQuietly(AutoCloseable resource ) {
      try {
        if (resource != null) {
          resource.close();
        }
      } catch( Exception ex ) {
        log( "Exception during closing the resources", ex );
      }
    }

you need to put the lines you want to execute in a finally statement, the finally statement will do what you want before throwing the exception. You can also check the autocloseable interface

If the first instruction invokes an exception, the rest of the program is not further executed. To solve that the finally -block was introduced to exception-handling. There you may release any resources.

That's exactly what the Java 7 try-with-resources new feature is for!

You just use it in this way:

try (
    final Type1 rs = ...;
    final Type2 ps = ...;
    final Connection connection = ...
) {
    ...
    ... // usefull code here
    ...
}
catch (...) {
    // exception handling
}

and automagically all is closed in any case (if your useful code throws an exception or if it does not throw), all the exceptions are handled, and you leave the block in a consistent state. The only thing is that all the classes ( Type1 , Type2 , Connection ) must implement the Autocloseable interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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