Reputation: 4631
I wrote an own Exception (named MyOwnException
) class in Java:
package com.stackoverflow.clojure;
import java.lang.RuntimeException;
public class MyOwnException extends RuntimeException {
private static final long serialVersionUID = 3020795659981708312L;
public MyOwnException(String s) {
super("My own exception says: " + s);
}
}
Now I'd like to use MyOwnException
within my Clojure code. I tried several things, But I always got Class not found exceptions (.java
and .clj
are in the same package com.stackoverflow.clojure
):
(ns com.stackoverflow.clojure.testForOwnExceptions)
;(import '[com.stackoverflow.clojure MyOwnException])
(defn casetest [x]
(case x
"a" "An a."
"b" "A b."
; (-> (clojure.core/format "Expression '%s' not defined." x)(MyOwnException.)(throw))
; (-> (clojure.core/format "Expression '%s' not defined." x)(com.stackoverflow.clojure.MyOwnException.)(throw))
(-> (clojure.core/format "Expression '%s' not defined." x)(IllegalArgumentException.)(throw))
))
;(prn(casetest "error"))
Besides that: how would a solution purely in Clojure look like? (definition of Exception class + usage)
Update (project.clj):
(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
:description "Tests of Clojure test-framework."
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[instaparse "1.3.4"]]
:source-paths ["src/main/clojure"]
:java-source-paths ["src/main/java"]
:test-paths ["src/test/clojure"]
:java-test-paths ["src/test/java"]
)
Upvotes: 2
Views: 544
Reputation: 11082
Using
(import '[com.stackoverflow.clojure MyOwnException])
should work together with
...
(-> (clojure.core/format "Expression '%s' not defined." x)(MyOwnException.)(throw))
...
Or the full qualified name alone:
...
(-> (clojure.core/format "Expression '%s' not defined." x)(com.stackoverflow.clojure.MyOwnException.)(throw))
...
Since you already had the correct code within your question (outcommented parts), I guess that something went wrong with your REPL. Maybe you should restart it before running one of the following code-combinations.
And keep in mind: only running (prn(casetest "error"))
does not automatically change the code of the function. You have to send the complete new code to the REPL (e.g. by using Run as/Clojure Application)
(ns com.stackoverflow.clojure.testForOwnExceptions)
(import '[com.stackoverflow.clojure MyOwnException])
(defn casetest [x]
(case x
"a" "An a."
"b" "A b."
(-> (clojure.core/format "Expression '%s' not defined." x)(MyOwnException.)(throw))
))
(prn(casetest "error"))
or
(ns com.stackoverflow.clojure.testForOwnExceptions)
(defn casetest [x]
(case x
"a" "An a."
"b" "A b."
(-> (clojure.core/format "Expression '%s' not defined." x)(MyOwnException.)(throw))
))
(prn(casetest "error"))
Upvotes: 1
Reputation: 20245
How you are running your code? Are you compiling your Java source code first? And after compilation, are Java class files are in class path?
I would suggest to use Leiningen and to use different structure for your source. Don't mix sources in one package. Have for an example src/main/java
and src/main/clojure'
.
Have a look at: Polyglot (Clojure, Java) Projects With Leiningen.
Upvotes: 2