Commits

Anonymous committed e13a8ab

fix `!` macro to re-throw the caught exception and catch a Throwable in the first place

  • Participants
  • Parent commits c1a9e3c

Comments (0)

Files changed (2)

File src/main/clj/org/bituf/clj_miscutil.clj

 
 (defmacro !
   "Execute given body of code in a try-catch block; if exception occurs then
-  print friendly stack trace.
+  print friendly stack trace followed by normal stack trace, and then re-throw
+  the exception.
   See also: print-exception-stacktrace, print-stacktrace"
   [& body]
   `(try ~@body
-     (catch Exception e#
+     (catch Throwable e#
+       ;; print pretty stack trace
        (print-exception-stacktrace e#)
-       (.printStackTrace e# ^PrintWriter *err*))))
+       ;; print normal stack trace too (because at times, user might want both)
+       (.printStackTrace e# ^PrintWriter *err*)
+       (throw e#))))
 
 
 (defmacro !!

File src/test/clj/org/bituf/test_clj_miscutil.clj

   (testing "with-err-str"
     (is (= "Hello" (with-err-str (binding [*out* *err*] (print "Hello")))))
     (let [txt (with-err-str
-                (! (/ 10 0)))
+                (maybe-ret (! (/ 10 0))))
           ltx (.substring ^String txt 0 45)]
       (is (= ltx "java.lang.ArithmeticException: Divide by zero"))))
   (testing "pprint-str"
     (let [st (with-err-str (print-exception-stacktrace (NullPointerException.)))]
       (is (and (string? st) (not-empty? st)))))
   (testing "!"
-    (let [st (with-err-str (! (/ 10 0)))]
+    (let [st (with-err-str (maybe-ret (! (/ 10 0))))]
       (is (and (string? st) (not-empty? st))))))