aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-07-11 13:51:11 +0000
committerscgilardi <scgilardi@gmail.com>2008-07-11 13:51:11 +0000
commit084b5693f329c1492837c5038aff15ccd476da25 (patch)
tree742f4e7edc898bf59a18775003efdbf34dfe4510
parentf72a57cfd8bd7fa6c9cff92e40c78d7868263205 (diff)
except.clj: simplify implementation
-rw-r--r--except.clj42
1 files changed, 24 insertions, 18 deletions
diff --git a/except.clj b/except.clj
index 1fd9c232..e89246cb 100644
--- a/except.clj
+++ b/except.clj
@@ -1,10 +1,10 @@
-;; Copyright (c) Stephen C. Gilardi. All rights reserved.
-;; The use and distribution terms for this software are covered by the
-;; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
-;; which can be found in the file CPL.TXT at the root of this distribution.
-;; By using this software in any fashion, you are agreeing to be bound by
-;; the terms of this license.
-;; You must not remove this notice, or any other, from this software.
+;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
+;; distribution terms for this software are covered by the Common Public
+;; License 1.0 (http://opensource.org/licenses/cpl.php) which can be found
+;; in the file CPL.TXT at the root of this distribution. By using this
+;; software in any fashion, you are agreeing to be bound by the terms of
+;; this license. You must not remove this notice, or any other, from this
+;; software.
;;
;; except.clj
;;
@@ -17,22 +17,28 @@
(lib/use string)
(defn throw-if
- "Throws an exception with a message if pred is true. Arguments are:
- pred class? format format-args*
- where class is optional and defaults to Exception, format is a string
- as documented for java.util.Formatter, and format-args are objects
- corresponding to format specifiers in format."
+ "Throws an exception with a message if pred is true. Arguments are:
+
+ pred class? format format-args*
+
+ class is optional and defaults to Exception. If present, it must be a
+ Class in the tree under Throwable with a constructor that takes a single
+ String.
+
+ format is a string as documented for java.util.Formatter.
+
+ format-args are zero or more objects that correspond to the format
+ specifiers in format."
[pred & args]
(if pred
- (let [[arg0 arg1 & more] args
- [class fmt fmt-args] (if (instance? Class arg0)
- [arg0 arg1 more]
- [Exception arg0 (cons arg1 more)])
+ (let [class-present (instance? Class (first args))
+ args (if class-present args (cons Exception args))
+ [class fmt & fmt-args] args
ctor (.getConstructor (identity class) (into-array [String]))
message (apply format fmt fmt-args)
exception (.newInstance ctor (into-array [message]))
raw-trace (.getStackTrace exception)
boring? #(not= (.getMethodName %) "doInvoke")
- trace (drop 2 (drop-while boring? raw-trace))]
- (.setStackTrace exception (into-array trace))
+ trace (into-array (drop 2 (drop-while boring? raw-trace)))]
+ (.setStackTrace exception trace)
(throw exception))))