Re: Readability of exception handling
Parker Jones <[email protected]> Tue, 8 Apr 2014 02:49:57 +1200
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
> Date: Wed, 2 Apr 2014 13:33:46 +0200 > From: [email protected] > > The pattern > > (catch(_, Exception, true) -> (var(Exception) -> .. ; ..) ; .. ) > > occurs frequently in the libraries. Could someone explain how this > > works? I understand there is no handler (just true/0) and then a check > > to see if Exception is instantiated. > > Exceptions can be any term, but not variables. I.e., throw(_) itself > is an error. catch(Goal, E, true) thus succeeds if Goal succeeds or > throws an error. Iff goal succeeded, E is a variable. It does the trick, but it's a bit of a roundabout way of expressing things. > > I find this syntax more accessible: > > try { } > > catch(e1) { } > > catch(e2) { } > > finally { } > > > > I don't suppose there is a way of writing exception handlers like this > > and translating to ISO compliant predicates? > > It is this: > > catch(Goal, E, true), > ( nonvar(E) > -> ( E = e1 --> ... > ; E = e2 --> ... > ; ... % finally > ) > ; true > ). Thank you Jan, this translation is very helpful. I keep it as a reference. Perhaps it's due to my inexperience that I have to decrypt my own code to understand it. Either that or it's simply not possible to write readable code beyond the simplest cases using ISO syntax exception handling. I'd be a lot happier with a bit of syntactic sugar. I'd be very interested to see something like a robust RPC server written in readable Prolog. There's a lot that can go wrong with an RPC call and it all has to be handled safely. Meanwhile I dread discovering a bug in my exception code in 12 months time and having to come back to it. Cheers, Parker -------------- next part -------------- HTML attachment scrubbed and removed