Announcement Announcement Module
Collapse
No announcement yet.
DataAccessException And SQLException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 04:28 PM.

  • #2
    You can throw a RuntimeException from within the ResultSetExtractor.

    Comment


    • #3
      @karldmoore

      You'd have a code chunk example ?

      Comment


      • #4
        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);
        }

        Comment


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

          Comment

          Working...
          X