Re: PSQLException instead of java.net.SocketException - Mailing list pgsql-jdbc

From Oliver Jowett
Subject Re: PSQLException instead of java.net.SocketException
Date
Msg-id 41D87E25.6000508@opencloud.com
Whole thread Raw
In response to PSQLException instead of java.net.SocketException  ("Nico" <nicohmail-postgresql@yahoo.com>)
List pgsql-jdbc
Nico wrote:

> My best guess is the exception was
> catched and a PSQLException was thrown in the catch block.

Yes, this is exactly what's happening. In general the driver will throw
a SQLException for all errors, including connection-level problems.

> I already have a
> catch block for PSQLException which is designed to warn me for SQL syntax
> errors, not socket exceptions. So I want to make an additional catch block
> for a java.net.SocketException. However I don't know how. Can someone please
> help me here?

You probably want to look at SQLException.getSQLState(). That gives a
somewhat standardized string that describes the error in more detail.
The driver passes through error codes received from the server directly,
and synthesizes appropriate error codes in some other cases (e.g.
connection failure).

In your case you want to look for all error codes starting with "08"
("Connection Exception"). See
http://www.postgresql.org/docs/current/static/errcodes-appendix.html for
a list of codes.

You could also do it by looking at the nested exception of the
SQLException, but that's not standard and isn't guaranteed to work; it's
really there to give extra information to a human, not for programmatic
logic.

-O

pgsql-jdbc by date:

Previous
From: Dave Cramer
Date:
Subject: Re: PSQLException instead of java.net.SocketException
Next
From: "Cameron Braid"
Date:
Subject: unsubscribe