title: How To Get SQLException reason from SQLException Object
body: <p>I am using JDBC, and am running a query. Is there a way to get the reason it failed from the SQLException object returned?</p>
  
  <p>In particular, I want to know if my query violated a foreign key constraint (and which one), or a key constraint.</p>
  
  <p>Would this result be vendor-specific? Just in case, I am using the postgresql-8.4-701.jdbc4.jar driver. If it is vendor-specific, where would I find the codes?</p>
  
  <p>EDIT: I want to do this dynamically - i.e. </p>
  
  <pre><code>if(violated foreign key constraint on attribute x) {
       return 5;
  } else if (violated primary key constraint) {
       return 7;
  } else {
       return 0;
  }
  </code></pre>
  
  <p>Or something like that.</p>
  
  <p>EDIT: According to <a href="http://archives.postgresql.org/pgsql-jdbc/2003-02/msg00173.php" rel="nofollow">this post</a>, there are no vendor-specific error codes for  PostgreSQL JDBC. Dunno if that's still valid.</p>