You have to build a class with gen-class.
Here's an example:
-------------------------------------------------------
(ns com.infolace.format.InternalFormatException
(:gen-class
:extends Exception
:init init
:constructors { [ String Integer ] [ String ] }
:state pos))
(defn- -init [msg level]
[[msg] level])
-------------------------------------------------------
The deal on gen-class is that you actually need to compile the class
files before they are useful. I use an ant task to do this. You can
see it here:
http://github.com/tomfaulhaber/cl-format/blob/0d436ab7026b74865df99bb26bafbbeed0726b4d/build.xml
It's probably a better idea to have your class inherit from
RuntimeException to avoid having it be rewrapped (though I think it
sometimes gets rewrapped anyway). This happens because Clojure doesn't
support checked exceptions (Rich has a very compelling explanation for
this).
In the end, I abandoned using custom exceptions. I was only using them
to carry custom information. Instead, I bind an error function that
takes the extra information as an argument and just I generate an
exception using that function if I have an error. (Thanks to Chouser
for brainstorming up that idea with me.)
I would love to see a better thought out way to be able to selectively
throw and catch since this is (to me) the big advantage of custom
exceptions. Might be done with a variation of proxy.
Hope that helps,
Tom
On Feb 4, 6:55 pm, Jeffrey Straszheim <[email protected]>
wrote:
> Is there an easy way to create custom exception classes. Like in Java
> where you do:
>
> public class MyOwnException {}
>
> I suppose there is a way to do it with gen-class, but gen-class looks
> complicated.
>
> Can someone post a simple example?
>
> (btw, this would make a great macro in clojure.contrib. I'd write it
> if someone tells me how to contribute a diff.)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---