πŸƒCheck vs Unchecked Exception

Describe about difference of check and unchecked exception

Check Exception is verified at run time so we will need to add throws on method.

Example

// Example Exception
1. IOException
2. SqlException

public void insert(User user) throws SqlException {
    // If we don't want to try catch then we must to add `throws` keyword.
    jdbcTemplate.update(user,new UserMapper());                                
}

For Unchecked Exception, developer is the person who verify it by using try-catch or ignore verifying .

Example

// Example Exception
1. NullPointerException
2. RuntimeException
3. ArithmeticException

// Requuest can be null
public void createUser(CreateUserRequest createUserRequest){
    try {
        String username = createUserRequest.getUserName();
    }catch(NullPointerException ex){
         throw new BusinessException("Request is null");   
    }        
}

Last updated