views:

705

answers:

3

have this method call -> simpleJdbcTemplate.queryForInt(sql,null); -> queryForInt() method in the springs SimpleJdbcTemplate throws a DataAccessException which is a runtime exception. i want to propegate exceptions to the view tier of the application since Spring frame work Wraps Checked Exceptions inside RuntimeExceptions i stuck here

how do i do this?

Explanation 1:

The value-add provided by the Spring Framework's JDBC abstraction framework- they say The Spring Framework takes care of all except 3 and 6. 3 and 6 need to be coded by an application developer

  1. Define connection parameters
  2. Open the connection
  3. Specify the statement
  4. Prepare and execute the statement
  5. Set up the loop to iterate through the results (if any)
  6. Do the work for each iteration
  7. Process any exception
  8. Handle transactions
  9. Close the connection

but if the exceptions not handled in the developer level if i encounter a situatoin like this: the connection to the database losses after certain time the program started. then a runtime exception will be thrown when a call to the above method made.since i dont handle the exception i cannot inform the user interface(view)

how do i solve this?

A: 

It depends if your view tier catches checked exceptions (any subclass of throwable that does not subclass RuntimeException or Error, or are not instances of RuntimeException or Error directly) or unchecked exceptions (RuntimeException or Errors or subclasses of these Throwable subclasses).

Generally, you'll either have something like this:

try {
//... processing
} catch(Exception/RuntimeException e) {
// propagate the exception to the view in a meaningful manner
}

If this is the case, for a runtime exception, you don't have to do anything - the block will catch the runtime exception.

If you want to convert it to checked, assuming you're using a version of Java that supports wrapped exceptions, all you have to do is:

try {
//...spring code
} catch(DataAccessException e) {
throw new Exception(e);
}

Then, your layer above this processing will catch it as a checked exception.

MetroidFan2002
Thanks very help full!
buddhi
+1  A: 

Do you just want to be able to access the original exception information in your View? If so, you should be able to invoke getCause() on the RuntimeException to get the underlying checked Exception that caused it. Otherwise you would need to add a "throws" declaration to your methods that are utilizing SimpleJdbcTemplate, catch DataAccessException and rethrow the checked Exceptions that are wrapped.

cliff.meyers
Thanks very help full!
buddhi
+5  A: 

Just because Spring throws a runtime exception doesn't mean you cannot catch it. If you want to do something special for DataAccessExceptions, you can certainly do that:

try {
    // query logic
} catch (DataAccessException ex) {
    // handle the exception
}

If you're using Spring's MVC framework, it may be worth looking into the ExceptionResolver interface. It's a mechanism for deciding how to handle all those uncaught exceptions thrown by the lower layers of the application. It gives you one last chance to display a different view based on exceptions that are thrown.

David
comment added so I can find this answer again.
toolkit