Re: Readability of exception handling
Jan Wielemaker <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 03/31/2014 06:09 PM, Parker Jones wrote: >> From: [email protected] >> >> catch(my_function(A,B,C), >> my_exception(D), >> ( writeln(my_exception(D)), >> fail). >> >> my_function(_,_,C) :- throw(my_exception(C)). > > Thanks for the reply, Alan. But that is one handler that catches > only one exception. What about exception_1 with handler_1, > exception_2 with handler_2,...? Does that have a flat structure or > does it require nesting? There is indeed a lot to say about proper exception handling in Prolog. Too often (me too), we see code such as catch(open(....), _, fail) which may fail because the file cannot be opened, but for zillions of other reasons, such as wrong arguments to begin with or even some resource error. If anyone is aware of a good story that describes best practices here, please provide a pointer. Some issues: - What error terms to throw from user code? - When to catch errors and when test that you won't get one? - When to use setup_call_cleanup/3 (instead) - How to deal with one specific exception? - How to deal with a variety of exceptions that may happen? - How and when to use print_message/2 - How to use library(prolog_stack)? This could make a great tutorial! I'm happy to write some parts and or comment! Cheers --- Jan > > > >> From: Parker Jones <[email protected]> >> To: "[email protected]" <[email protected]> >> Sent: Monday, March 31, 2014 11:19:07 AM >> Subject: [SWIPL] Readability of exception handling >> >> >> Hello all, >> >> I am trying to write some exception handling code that is readable but it always looks unnecessarily complicated. Could someone point me to some code examples of exception handling that are elegant? All the examples I have come across, apart from the very simplest cases, look like a submission to an obfuscation contest. >> >> Also, is there a reason Prolog textbooks avoid covering exception handling? >> >> Thanks for any suggestions, >> Parker >> >> -------------- next part -------------- >> HTML attachment scrubbed and removed >> _______________________________________________ >> SWI-Prolog mailing list >> [email protected] >> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog >> -------------- next part -------------- >> HTML attachment scrubbed and removed >> _______________________________________________ >> SWI-Prolog mailing list >> [email protected] >> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog > > -------------- next part -------------- > HTML attachment scrubbed and removed > _______________________________________________ > SWI-Prolog mailing list > [email protected] > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog >