Custom Exceptions in Clojure?

前端 未结 3 2027
醉话见心
醉话见心 2021-02-02 06:11

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/w

3条回答
  •  庸人自扰
    2021-02-02 06:29

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

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

    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/:......."
    

    Compile your class:

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

    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
    

    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.)
    #
    

提交回复
热议问题