Clojure中的自定义异常? [英] Custom Exceptions in Clojure?

查看:148
本文介绍了Clojure中的自定义异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Clojure中创建一个用户定义的异常,并且一直存在各种各样的问题。我尝试了这里概述的方法:

I've been trying to create a user-defined exception in Clojure, and have been having all sorts of problems. I tried the method outlined here:

http://en.wikibooks.org/wiki/Clojure_Programming/Concepts#User-Defined_Exceptions

(gen-and-load-class 'user.MyException :extends Exception)

但是不似乎在Clojure 1.2中工作(或者我做错了...)。我的环境是Clojure 1.2,Emacs和lein swank。

But that doesn't seem to work in Clojure 1.2 (or I'm doing something wrong...). My environment is Clojure 1.2, Emacs, and lein swank.

感谢您的帮助!

推荐答案

创建一个文件 src / user / MyException.clj (其中 src 在<$包含:

Make a file src/user/MyException.clj (where src is on CLASSPATH) containing:

(ns user.MyException
  (:gen-class :extends java.lang.Exception))

检查 * compile-path * 在REPL。确保此目录存在,并且位于 CLASSPATH 上。创建目录如果不存在; Clojure不会为你做的。

Check the value of *compile-path* at the REPL. Make sure this directory exists and is on CLASSPATH. Create the directory if it doesn't exist; Clojure won't do so for you.

user> *compile-path*
"/home/user/foo/target/classes/"
user> (System/getProperty "java.class.path")
".......:/home/user/foo/target/classes/:......."

编译你的课程:

user> (compile 'user.MyException)
user.MyException

如果工作,在code> * compile-path * 你现在应该有这样的文件:

If it worked, in *compile-path* you should now have files something like this:

/home/user/foo/target/
/home/user/foo/target/classes
/home/user/foo/target/classes/user
/home/user/foo/target/classes/user/MyException.class
/home/user/foo/target/classes/user/MyException__init.class
/home/user/foo/target/classes/user/MyException$loading__4410__auto__.class

重新启动Clojure REPL / JVM加载这些类。再次确保这些新的类文件位于 CLASSPATH 上。现在你应该能够使用你的课程:

Restart your Clojure REPL / JVM to load these classes. Again, make sure these new class files are on CLASSPATH. Now you should be able to use your class:

user> (user.MyException.)
#<MyException user.MyException>

这篇关于Clojure中的自定义异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆