Clojure:如何从clojure / leiningen项目访问SQLException?

时间:2022-08-31 22:49:49

I have a sql exception from clojure that goes like this:

我有一个来自clojure的sql异常,如下所示:

java.lang.Exception: transaction rolled back: 
  Batch entry 0 drop database triface was aborted.  
Call getNextException to see the cause.

I want to call getNextException on the exception I receive:

我想在收到的异常上调用getNextException:

(require [clojure.contrib.sql :as sql])

(try 
  (db/rebuild-table) ;; function causing the exception
  (catch Exception e (.getNextException e)))

But then I get this:

但后来我明白了:

java.lang.IllegalArgumentException: No matching field found: 
  getNextException for class java.lang.Exception

So I assume I want to catch a SQLException:

所以我假设我想要捕获一个SQLException:

(catch SQLException e (.getNextException e)))

Except I can't find how to require this class. It does not seem to be provided by either clojure.contrib.sql or clojure.contrib.sql.internal. The javadocs say it is in java.sql, but how do I require that?

除了我找不到如何要求这门课。它似乎不是由clojure.contrib.sql或clojure.contrib.sql.internal提供的。 javadocs说它在java.sql中,但我该如何要求呢?

Thanks!

1 个解决方案

#1


2  

You don't require it, you import it,

您不需要它,导入它,

(import java.sql.SQLException)

but even if you catch a Exception as long as it is a SQLException you should be able to call getNextException

但即使你捕获一个Exception,只要它是一个SQLException你应该能够调用getNextException


(let [up (SQLException. "throw")]
  (try
    (throw up)
    (catch Exception e (isa? (type e) SQLException))))

#1


2  

You don't require it, you import it,

您不需要它,导入它,

(import java.sql.SQLException)

but even if you catch a Exception as long as it is a SQLException you should be able to call getNextException

但即使你捕获一个Exception,只要它是一个SQLException你应该能够调用getNextException


(let [up (SQLException. "throw")]
  (try
    (throw up)
    (catch Exception e (isa? (type e) SQLException))))