Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a simple try / catch with clojure?

Tags:

Neither of the code snippets below work with clojure 1.6.0

(try  (prn vv)   (catch clojure.lang.ExceptionInfo e (prn "catch e: " e))   (finally (prn "finally") ) ) 

I also tried this, after reading some other posts here :

(try  (doall (prn vv))   (catch clojure.lang.ExceptionInfo e (prn "catch e: " e))   (finally (prn "finally") ) ) 

What am I missing ?

Mike

like image 901
mstram Avatar asked Aug 08 '14 20:08

mstram


People also ask

How do you implement a try catch?

The “try… It works like this: First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .

Can we use try catch without throw?

Yes it does not have throw or rethrow activity. I put the try catch in the processing part. Then I would recommend you to put a debugger inside catch and see which activity or segment is throwing the exception.

Is try catch faster than if?

If you've one if/else block instead of one try/catch block, and if an exceptions throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster.

What is a Trycatch?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.


1 Answers

Catching ExceptionInfo will only catch throwables created with ex-info:

(try   (throw (ex-info "bad" {:a 1 :b 2}))   (catch clojure.lang.ExceptionInfo e     (prn "caught" e))) ;; => "caught" #<ExceptionInfo clojure.lang.ExceptionInfo: bad {:b 2, :a 1}> 

But not other Exceptions:

(try   (/ 1 "0")   (catch clojure.lang.ExceptionInfo e     (prn "caught" e))) ;; => ClassCastException java.lang.String cannot be cast to java.lang.Number  clojure.lang.Numbers.divide (Numbers.java:155) 

Which you would catch like this:

(try   (/ 1 "0")   (catch Exception e     (prn "caught" e))) ;; => "caught" #<ClassCastException java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number> 

Compilation exceptions happen before evaluation:

(try   w is undefined   (catch Exception e     (prn "caught" e))) ;; => CompilerException java.lang.RuntimeException: Unable to resolve symbol: w in this context, compiling:(/private/var/folders/8r/y2d3thln6s3fyrl_s6vknb440000gn/T/form-init6601588363347445596.clj:1:5)   (try   (eval 'w)   (catch Exception e     (prn "caught" e))) ;; => "caught" #<CompilerException java.lang.RuntimeException: Unable to resolve symbol: w in this context, compiling:(/private/var/folders/8r/y2d3thln6s3fyrl_s6vknb440000gn/T/form-init6601588363347445596.clj:1:5)> 

You can also have multiple catch clauses to handle different types of exceptions.

like image 179
Timothy Pratley Avatar answered Sep 30 '22 19:09

Timothy Pratley