0

I want to throw any exception that occurs while doing MySQL transaction to the application. But before that I want to close any resources that are in open state. But closing these resources might again generate exception which again I would want to report to application. The following code will make this a little clear:

try
{
   // connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
   // throw this exception by wrapping it in another user defined exception class
}
finally
{
   try
   {
      // close resources opened in try block ( statement, connection )
   }
   catch ( Exception e )
   {
      // throw this exception by wrapping it in another user defined exception class
   }   
}

I want to know what is the correct way to handle this situation ( having two exception being thrown ). Thanks for your help.

2
  • Which version of Java you are working on? Commented Aug 21, 2014 at 9:17
  • The java version I am using is 1.6.0_22 Commented Aug 21, 2014 at 10:43

3 Answers 3

1

I suggest you to use Java 7 The try-with-resources Statement

It's better explained in Oracle Documentation on


sample code:

try(  Connection conn = dataSource.getConnection();
      PreparedStatement stmt = conn.prepareStatement(query);
      ResultSet rs = stmt.executeQuery() ) {
     // connection, statements and result set are automatically closed 
}

Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks to me a promising solution. We are still using Java 6 here but I will definitely find out more about this.
1

You may try like this:

try
{
   // connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
   // throw this exception by wrapping it in another user defined exception class
}
finally
{
  closeMyConnection(resource);
}

protected void closeMyConnection( Resource resource ) {
  try {
    if (resource != null) {
      resource.close();
    }
  } catch( Exception ex ) {
    log( "There is some excetion", ex );
  }
}

1 Comment

Thanks R.T. for you help. Basically then what should happen is that any exception thrown within finally should get handled where it is thrown and the original exception ( or any other return value ) should be passed back to application. I hope this is how most applications would work. Alas I can mark only one solution as accepted. But thanks to everyone for their efforts and help.
1

try

Exception lastException = null:
try
{
   // connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
   // throw this exception by wrapping it in another user defined exception class
   lastException = e;
}

if (lastException != null) {
  // you know what to do
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.