Handling an error thrown by a MySQL trigger in Controller class Spring Boot

Viewed 44

I'm a bit confused on how to handle the error that my trigger generates in the database. In the future, I plan to add the text of this error to the page on which it was processed. IDEA writes that "Exception 'java.sql.SQLException' is never thrown in the corresponding try block"

@PostMapping("/register")
public String addNewUser(User user, Details details) {
    user.setPassword(passwordEncoder.encode(user.getPassword()));
    user.setRole(Role.USER);
    details.setUser(user);
    user.setDetails(details);
    try {
        userRepository.save(user);
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }

    return "redirect:/";
}
1 Answers

The save method of jpa interface never throws SQLException , thus you cannot catch it. You can only catch an exception when your try block can possibly generate an exception of that kind. You can refer to this answer for better understanding.

Related