\0 and IllegalArgumentException

From: Sebastiaan van Erk <sebster(at)sebster(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: \0 and IllegalArgumentException
Date: 2004-10-29 09:55:10
Message-ID: 1099043710.595.50.camel@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi,

I'm using postgres 7.4 with the latest JDBC driver, and had an insert
statement to insert data as follows

try
{
...
ps.setCharacterStream(1, myStream, myStreamLength);
ps.executeUpdate();
}
catch (SQLException e)
{
...
}

With this I was hoping to catch invalid inserts (with an SQLException),
however I'm getting:

java.lang.IllegalArgumentException: \0 not allowed

which is an UNCHECKED runtime exception, that in no way indicates
(programatically) that the insert failed.

I think very few people would expect a runtime exception on what
essentially amounts to a failed insert. Futhermore, it is very hard to
discover the SOURCE of the runtime exception (lots of things might throw
an IllegalArgumentException) and to programatically do something
sensible with this.

Thus in my opinion, an SQLException would be better here for at least
three reasons:
1) It's a checked exception (I'm counting on it happening)...
2) I will know where it came from (postgres)...
3) Now I'm totally unsure of what other kind of runtime exceptions the
postgres driver may throw for some reason or other, nor do I know how to
handle a runtime exception properly if one does occur... I want runtime
exceptions basically only on PROGRAMMATIC errors, errors that I as the
developer made and can fix. User data CAN in fact contain \0, and the
fact that postgres cannot handle this is an issue of the backend, which
(in the case of postgres, but certainly not in the case of all
databases) does not consider these kind of strings as valid. As the
developer (oblivious to what kind of backend database is used) I cannot
take any programmatic steps to avoid this problem (other than string
scanning caught IllegalArgumentExceptions for this specific problem).

In conclusion, I REALLY think that this is the wrong exception to be
thrown.

Greetings,
Sebastiaan van Erk

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Giampaolo Tomassoni 2004-10-29 10:00:08 Re: Serializable transactions and SQLException
Previous Message Kris Jurka 2004-10-29 08:44:35 Re: Serializable transactions and SQLException