Two Laptop Bag*
The moose likes Clojure and the fly likes Exceptions in Clojure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Exceptions in Clojure " Watch "Exceptions in Clojure " New topic
Author

Exceptions in Clojure

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi guys :

I noticed that clojure's Exceptions have a "return" value.

This is odd to me, since technically java exceptions don't return anything .

For example

( try (pop '())(catch IllegalStateException e "nnnnnn"))

would actually "return" a "nnnnnn";


Can somebody explain

1) How the "return" mechanism is working here ? and
2) How the syntax of 'catch IllegalStateException e "nnnnn"' is actually working ? Its kind of strange because, I can't tell wether the catch is "pure" clojure, or just syntactic sugar that is wrapping the java exception handling syntax.


Thanks !!!
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 252
    
    5

In Clojure, everything is an expression that yields a value - there are no "statements".

Therefore a try/catch form will yield a value. Either the value of the try part (if that succeeds) or the value of the catch part (if an exception is caught). You can also specify a finally part to do any resource cleanup but the value of that part is ignored (in favor of the try value or the catch value).

Strictly speaking the try part allows for a sequence of expressions, just like do, let, function bodies and a few other forms. The value returned is the value of the last expression. Similarly for the catch part, which starts with an exception class and a var that is bound to the value of the caught exception (so you can access the exception details in the catch part). In that respect, it's just like the Java counterpart:

The equivalent Clojure:

Does that help?
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
yes. meanwhile, however I see you mentioned "let"..... Im not quite sure how "let" is working. I'm used to the python "let", which can be used to incrementally "offer" some output to the calling method. When I see "let" used in clojure, however, I fail to see the parallel.
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 252
    
    5

Think of let as a block that introduces local variables:

This introduces a series of bindings of names to values, which are then available only inside the lexical scope of the let form. In Scala you might have:

or in Java:

Hope that helps explain let?
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Okay... your being so helpful i figured id shoot another one out there ....
1) ive seen alot of code with "if" and "do". However, not much "if " "else" . Is the "if/else" paradigm an unclojurely concept ?

and

2) Why is it that let is nested ? for exmple, in the below function, there is an "if" after "let".... Does that mean that the "if" function is an "argument" to let ???

for example :


why no else here ?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 252
    
    5

The if form can have two or three parts: the conditional, an expression to evaluate if truthy and an optional expression to evaluate otherwise (if falsey):

The reason you may see if-do is because if can only take a single expression for the truthy situation (and a single expression for the falsey situation). If it's just if and multiple expressions, you'll most likely see when instead:

That's equivalent to the following expression where the do form lets you evaluate multiple expressions and just return the last one:

In your code example, let just introduces a local binding for past-visitor that is available in the body of the let (the if form). Your if has a condition, past-visitor, an expression to evaluate if truthy (returning the Welcome back string) and an expression to evaluate if falsey (a do form with two expressions, updating the ref visitors and then returning the Hello string).

On a point of style, Lisp / Clojure developers do not use that trailing brace style. You're going to see this instead:

Since most editors used by Lisp / Clojure developers automatically match parens, folks tend to work with stacked parens rather than the "matched brace" style that you usually see in C-style languages like Java. The stacked parens style, where structure is clearly shown with indentation, also makes it clear that the nested if has three expressions and therefore has an "else" expression.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions in Clojure
 
Similar Threads
SimpleDateFormat parses illegal date string
Taking the Next Step
clojure koans won't return to command prompt after executing
issue realted to IllegalStateException
Will Clojure be popular among Java programmers?