Note that there are some explanatory texts on larger screens.

plurals
  1. POPSQLException not getting caught
    text
    copied!<p>I am using Tomcat 6 and Postgresql 8.4. My code looks as follows:</p> <pre><code>try { // Prepared statement inserting something... } catch (final PSQLException e) { LOG.log(Level.SEVERE, "Saving failed.", e); } catch (final SQLException e) { LOG.log(Level.SEVERE, "Saving failed (SQL).", e); } </code></pre> <p>This insert into DB may cause a PSQLException (e.g. a unique key violation) which I want to catch with the first <code>catch</code>. However, what I actually find in the log is this:</p> <pre><code>SEVERE: Saving failed (SQL). org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "UZIVATELIA_U_LOGIN" at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at cz.ecoremedia.realpad.web.backend.Users.saveNewUser(Users.java:119) at cz.ecoremedia.realpad.web.backend.Users.saveUser(Users.java:237) at org.apache.jsp.User_jsp._jspService(User_jsp.java:159) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662) </code></pre> <p>If I check the type of <code>e</code> I caught, for example like this:</p> <pre><code>} catch (final SQLException e) { LOG.log(Level.SEVERE, e.getClass().getName()); LOG.log(Level.SEVERE, "Saving failed (SQL).", e); </code></pre> <p>It still tells me it was <code>org.postgresql.util.PSQLException</code>.</p> <p>When I try this locally in my Eclipse, it works perfectly - <code>PSQLException</code> gets caught correctly in the first <code>catch</code> block.</p> <p>If I understand the problem correctly, the <code>org.postgresql.util.PSQLException</code> I catch is a different class from the <code>org.postgresql.util.PSQLException</code> that actually gets thrown. How is this possible? What am I doing wrong?</p> <p>Thanks for your ideas.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload