Clojure/PostgreSQL:当数据库中出现错误时,如何处理异常?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (58)

在PostgreSQL中使用Clojure时,每当我发出的语句以某种方式出现错误或被数据库本身拒绝时,我就会得到类似于以下错误的信息:

java.sql.BatchUpdateException: 
  Batch entry 0 drop schema public cascade was aborted.  
  Call getNextException to see the cause.

如何调用getNextException,以便能够看到我做错了什么?

提问于
用户回答回答于

在这个try CATCH块中,您可以:

(.printStackTrace (.getCause e))
用户回答回答于

(defn drop-table [name]
    (sql/with-connection db
      (with-open [s (.createStatement (sql/connection))]
        (try
          (.addBatch s (str "drop table " name ))
          (seq (.executeBatch s))
          (catch Exception _)))))

(defn setup []
  (comment "TODO: create tables here"))

(defn -main []
  (try
  (print "Dropping table...") (flush)
  (drop-table "table_name")
  (print "Creating database structure...") (flush)
  (setup)
  (println " done")
  (catch Exception e (.getNextException e))))

扫码关注云+社区