pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedCatching postgres exceptions - functions returning error values?
3+ messages / 2 participants
[nested] [flat]
* Catching postgres exceptions - functions returning error values?
@ 2006-08-30 14:38 Mario Splivalo <mario.splivalo@mobart.hr>
2006-08-30 16:27 ` Re: Catching postgres exceptions - functions returning error values? Marc Herbert <Marc.Herbert@continuent.com>
0 siblings, 1 reply; 3+ messages in thread
From: Mario Splivalo @ 2006-08-30 14:38 UTC (permalink / raw)
To: pgsql-jdbc
What would be the preffered way of catching postgres exceptions within
Java code?
For instance, I have an application where user types in database name
he/she wishes to connect to. So, when user enters the database name, I
try to connect to it. If that database does not exists, I'm thrown an
exception, something like this:
org.postgresql.util.PSQLException: Backend start-up failed:
org.postgresql.util.PSQLException: FATAL: database "testdb" does not
exist
Now, if the database server is unavailable, I get something like this:
org.postgresql.util.PSQLException: Connection refused. Check that the
hostname and port are correct and that the postmaster is accepting
TCP/IP connections.
I would like to know, in my code, what happened, and inform user
apropriatley, I don't want just to print the exception to the user.
I could parse the return from printStackTrace method, but that just
doesn't seem right.
Also, some of the functions need to return errorcode of some sort. If
the function is returning SETOF, I can't use OUT parametars. So, I'm
asking for a recomendation here.
I'm using postgres 8.1 with latest JDBC driver.
Thank you in advance,
Mario
--
Mario Splivalo
Mob-Art
mario.splivalo@mobart.hr
"I can do it quick, I can do it cheap, I can do it well. Pick any two."
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Catching postgres exceptions - functions returning error values?
2006-08-30 14:38 Catching postgres exceptions - functions returning error values? Mario Splivalo <mario.splivalo@mobart.hr>
@ 2006-08-30 16:27 ` Marc Herbert <Marc.Herbert@continuent.com>
2006-08-31 09:22 ` Re: Catching postgres exceptions - functions returning Mario Splivalo <mario.splivalo@mobart.hr>
0 siblings, 1 reply; 3+ messages in thread
From: Marc Herbert @ 2006-08-30 16:27 UTC (permalink / raw)
To: pgsql-jdbc
Mario Splivalo <mario.splivalo@mobart.hr> writes:
> Now, if the database server is unavailable, I get something like this:
>
> org.postgresql.util.PSQLException: Connection refused. Check that the
> hostname and port are correct and that the postmaster is accepting
> TCP/IP connections.
>
> I would like to know, in my code, what happened, and inform user
> apropriatley, I don't want just to print the exception to the user.
What would like to substitute to the above message for instance? Is
this just a language issue?
> I could parse the return from printStackTrace method, but that just
> doesn't seem right.
>
> Also, some of the functions need to return errorcode of some sort. If
> the function is returning SETOF, I can't use OUT parametars. So, I'm
> asking for a recomendation here.
Maybe you want to use the SQLState
http://www.postgresql.org/docs/8.1/interactive/errcodes-appendix.html
http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcodbc_error_codes.asp
http://www.opengroup.org/bookstore/catalog/c451.htm (appendix A)
http://www.opengroup.org/bookstore/catalog/c449.htm (appendix B)
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Catching postgres exceptions - functions returning
2006-08-30 14:38 Catching postgres exceptions - functions returning error values? Mario Splivalo <mario.splivalo@mobart.hr>
2006-08-30 16:27 ` Re: Catching postgres exceptions - functions returning error values? Marc Herbert <Marc.Herbert@continuent.com>
@ 2006-08-31 09:22 ` Mario Splivalo <mario.splivalo@mobart.hr>
0 siblings, 0 replies; 3+ messages in thread
From: Mario Splivalo @ 2006-08-31 09:22 UTC (permalink / raw)
To: pgsql-jdbc
On Wed, 2006-08-30 at 18:27 +0200, Marc Herbert wrote:
> > Also, some of the functions need to return errorcode of some sort. If
> > the function is returning SETOF, I can't use OUT parametars. So, I'm
> > asking for a recomendation here.
>
> Maybe you want to use the SQLState
>
> http://www.postgresql.org/docs/8.1/interactive/errcodes-appendix.html
>
> http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcodbc_error_codes.asp
>
> http://www.opengroup.org/bookstore/catalog/c451.htm (appendix A)
> http://www.opengroup.org/bookstore/catalog/c449.htm (appendix B)
>
Thank you! :) Although I've found out that errcodes-appendix.html from
postgresql manual doesn't contain some errors, for instance:
org.postgresql.util.PSQLException: Connection refused. Check that the
hostname and port are correct and that the postmaster is accepting
TCP/IP connections.
at
org.postgresql.jdbc1.AbstractJdbc1Connection.openConnection(AbstractJdbc1Connection.java:204)
at org.postgresql.Driver.connect(Driver.java:139)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at Example1.main(Example1.java:31)
--------------------------
Message: Connection refused. Check that the hostname and port are
correct and that the postmaster is accepting TCP/IP connections.
SQLState: 08004
ErrorCode: 0
This, 08004 is not listed there.
Then, if I throw some 'custom' exception from postgres (using plpgsql
RAISE EXCEPTION), I'll get SQLState code P0001 from getSQLState(). If I
want to know what realy happened, I need to parse what getMessage()
returns. Is there another way?
Mario
--
Mario Splivalo
Mob-Art
mario.splivalo@mobart.hr
"I can do it quick, I can do it cheap, I can do it well. Pick any two."
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-08-31 09:22 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-08-30 14:38 Catching postgres exceptions - functions returning error values? Mario Splivalo <mario.splivalo@mobart.hr>
2006-08-30 16:27 ` Marc Herbert <Marc.Herbert@continuent.com>
2006-08-31 09:22 ` Re: Catching postgres exceptions - functions returning Mario Splivalo <mario.splivalo@mobart.hr>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox