:pre and :post throwing Exception - is this a smell?

168 views
Skip to first unread message

Mark Derricutt

unread,
Dec 23, 2009, 5:03:24 PM12/23/09
to clo...@googlegroups.com
'lo,

I was reading http://blog.fogus.me/2009/12/21/clojures-pre-and-post/
on the new pre and post conditions and seeing that they throw
java.lang.Exception kinda struck me as a bad smell, esp. when
integrating with other systems written in Java or another language.
Forcing upstream clients to catch the top level Exception seems rather
rude on clojures part.

Was there any reason java.lang.Exception was used over say
java.lang.AssertionError? Or a more specific
PreConditionAssertionError/PostConditionAssertionError pair?

Mark

--
Pull me down under...

Sean Devlin

unread,
Dec 23, 2009, 6:52:11 PM12/23/09
to Clojure
It would be nice if the exception string stated if it was a pre or
post condition failure at the very least.

On Dec 23, 5:03 pm, Mark Derricutt <m...@talios.com> wrote:
> 'lo,
>
> I was readinghttp://blog.fogus.me/2009/12/21/clojures-pre-and-post/

Mike Douglas

unread,
Dec 24, 2009, 1:08:47 AM12/24/09
to Clojure
Hi,

I've written a related patch that makes three changes:

1. assert now raises AssertionError, instead of Exception.
2. assert is now overloaded with a second parameter that replaces
"Assert failed" in the original message.
3. the (pre- or post-) condition that raised the exception, along with
the form responsible, is now reported.

Previously:
"Caused by: java.lang.Exception: Assert failed: (= % x)"

Now:
"Caused by: java.lang.AssertionError: Postcondition failed when (bad-
fn 2): (= % x)"

Assembla seems to be having some problems, so I'm hosting it at
http://fanatico.org/better-pre-post.diff until they get that sorted
out.

Thoughts?

Daniel Werner

unread,
Dec 24, 2009, 4:04:37 PM12/24/09
to Clojure
On Dec 24, 7:08 am, Mike Douglas <mike.doug...@gmail.com> wrote:
> Previously:
> "Caused by: java.lang.Exception: Assert failed: (= % x)"

With your patch, assert does not include x in its message anymore in
case s is not given. Also, as a non-native speaker I find the phrase
"failed when" somewhat strange to my ears. Aside from this, your patch
immediately makes clear what went wrong.

Rich Hickey

unread,
Dec 29, 2009, 2:40:40 PM12/29/09
to clo...@googlegroups.com
On Wed, Dec 23, 2009 at 5:03 PM, Mark Derricutt <ma...@talios.com> wrote:
> 'lo,
>
> I was reading http://blog.fogus.me/2009/12/21/clojures-pre-and-post/
> on the new pre and post conditions and seeing that they throw
> java.lang.Exception kinda struck me as a bad smell, esp. when
> integrating with other systems written in Java or another language.
> Forcing upstream clients to catch the top level Exception seems rather
> rude on clojures part.
>

Forcing is the problem of Java's checked exceptions. Without them,
there would never be forcing.

> Was there any reason java.lang.Exception was used over say
> java.lang.AssertionError?  Or a more specific
> PreConditionAssertionError/PostConditionAssertionError pair?
>

But yes, java.lang.AssertionError is a better fit and I've changed
assert to throw that.

Rich

Reply all
Reply to author
Forward
0 new messages