Catching java method call exceptions

43 views
Skip to first unread message

Allen Rohner

unread,
Aug 11, 2008, 1:48:04 PM8/11/08
to Clojure
I have a slightly modified version of sql/execute-prepared-statement
from clojure-contrib/sql.clj:

(defn execute-prepared-statement
"Executes a prepared statement with a sequence of parameter sets"
[con sql sets]
(with-open stmt (.prepareStatement con sql)
(try
(doseq set sets
(doseq [index value] (map vector (iterate inc 1) set)
(.setObject stmt index value))
(.addBatch stmt ))
(. stmt (executeBatch))
(catch java.sql.SQLException e
(println "caught SQL Exception:" (. e (getNextException))))
(catch java.lang.Exception e
(println "caught Exception:" e " cause=" (. e (getCause)))
(. e (printStackTrace))))))

When I pass in a SQL command that causes an exception (invalid SQL,
violate constraint, etc), I would expect that the (catch
java.sql.SQLException...) would catch the exception. What actually
happens is that I catch a java.lang.reflect.InvocationTargetException
in the second clause.

The invocationTargetException has a cause (. e (getCause)) of
java.sql.BatchUpdateException, the exception I expected to catch.

Here's the stack trace:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:71)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:26)
at clojure.fns.sql.execute_prepared_statement__3140.invoke(sql.clj:
58)
at clojure.fns.user.new_session__2571.invoke(Unknown Source)
at clojure.lang.AFn.applyToHelper(AFn.java:184)
at clojure.lang.AFn.applyTo(AFn.java:175)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2569)
at clojure.lang.Compiler.eval(Compiler.java:3780)
at clojure.lang.Repl.main(Repl.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into
es.sessions (id, user_id) VALUES (41, 1) was aborted. Call
getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement
$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:
1328)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:
351)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:
2674)
... 18 more
caught Exception: java.lang.reflect.InvocationTargetException cause=
java.sql.BatchUpdateException: Batch entry 0 insert into es.sessions
(id, user_id) VALUES (41, 1) was aborted. Call getNextException to
see the cause.

Rich Hickey

unread,
Aug 11, 2008, 2:02:55 PM8/11/08
to Clojure
Reflector has had InvocationTargetException unwrapping since rev 949 -
what version are you using?

Rich

Allen Rohner

unread,
Aug 11, 2008, 2:16:10 PM8/11/08
to Clojure

> > The invocationTargetException has a cause (. e (getCause)) of
> > java.sql.BatchUpdateException, the exception I expected to catch.
>
> Reflector has had InvocationTargetException unwrapping since rev 949 -
> what version are you using?
>

Ah, that's what it was. I was using the (older) prepacked version that
ships in compojure. Using SVN head I got the behavior I expected.
Sorry for the trouble.

Thanks,
Allen
Reply all
Reply to author
Forward
0 new messages