Results 1 to 5 of 5

Thread: DataAccessException And SQLException

  1. #1
    Join Date
    Nov 2006
    Posts
    12

    Default DataAccessException And SQLException

    Hi all ,

    How can the following be achieved in Spring?
    I have to go through the resultSet and based on Business validations, I need to throw a custom exception, so that the layers above can map it to a proper error message for the User.
    As resultsetExtractor throws only SQLException which gets wrapped by the JDBCTemplate to DataAccessExceptions, how can I achieve this?
    Thanks for your help.

    Code:
    try
    {
    getJdbcTemplate().query(query,searchParams, new ResultSetExtractor() {
    
    	public Object extractData(ResultSet rs) throws SQLException {
    		while (rs.next()) {
    										}return null;
    	}
    	});
    }catch(DataAccessException){
    }
    Last edited by sreepriyar; May 1st, 2007 at 03:28 PM.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You can throw a RuntimeException from within the ResultSetExtractor.

  3. #3
    Join Date
    Mar 2008
    Posts
    250

    Default

    @karldmoore

    You'd have a code chunk example ?
    Stephane

  4. #4

    Default

    Karldmoore you would throw a runtime exception the same as any other exception. If your custom exception is a checked exception and not a runtime exception then throw your custom exception out of the catch block for the DataAccessException.

    btw DataAccessException is a runtime exception



    Code:
    Code:
    
    try
    {
    getJdbcTemplate().query(query,searchParams, new ResultSetExtractor() {
    
    	public Object extractData(ResultSet rs) throws SQLException {
    		while (rs.next()) {
    	//throw exception here if business rules are not validated successfully
    // any runtime exception will do
    throw new DataAccessException("my message");
    									}return null;
    	}
    	});
    }catch(DataAccessException dae){
    //catch it here and rethrow your custom exception  forget this step if your custom exception is a runtime exception //you can just throw it above
    throw new CustomException(dae.getMessage(), dae);
    }

  5. #5
    Join Date
    Mar 2008
    Posts
    250

    Default

    Having a run time custom exception makes it easier to let it bubble up to where I want to catch it too.
    Stephane

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •