How to handle a PSQLException in java?

后端 未结 4 945
孤街浪徒
孤街浪徒 2021-02-09 08:43

I have a unique constraint on one of my entities and whenever I get a PSQLException which occurs whenever that constraint is violated, I want to respond with a bad request.

4条回答
  •  庸人自扰
    2021-02-09 09:13

    You are catching PSQLException. Instead of that, please catch SQLException. With SQLException you will can handle all this SQL exceptions.

    You can check the SQLException knowledge at this link

    Then in your code just treat the SQLException as you want. The most generic catch clause is the following one:

        catch (SQLException e)
       {
       System.out.println("ERROR: Fetch statement failed: " +
          e.getMessage());
       }
    

    With this code you are printing the exception. If you want more information, check this

提交回复
热议问题