Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Richard Cole <rj-cole@iinet.net.au>
To: caml-list@inria.fr
Cc: ocaml-lib-devel@lists.sourceforge.net
Subject: [Caml-list] Re: Exceptions considered harmful
Date: Sun, 11 Jul 2004 23:12:34 +1000	[thread overview]
Message-ID: <ccree9$l6$1@sea.gmane.org> (raw)
In-Reply-To: <1088470968.18587.123.camel@pelican.wigram>

When I read your examples I wondered how often this situation arises. Is 
it just a symptom of the file interface or is it more general? It occurs 
whenever resources, such as a file descriptor, need to be relinquished, 
which generally is pretty often.

In one programming task that I worked on for some time most of the code 
followed the following pattern.

   1. allocate resources
   2. manipulate resources
   3. free resources

In each step you have to be ready for something to go wrong. Since it 
was in a distributed environment steps 1 2 and 3 were composed of 
concurrent parts, i.e. all resources were allocated concurrently.

In other programming tasks it has not been important to catch errors 
because the effect of the programming stopping or leaking a few 
resources is not serious.

But if you have to catch the errors it is tedious and error prone to 
write massive try catch blocks to catch every error. Our solution was to 
use the command pattern.

type success_or_failure = Success | Failure
class type command =
   method do: clipboard -> success_or_failure
   method undo: clipboard -> unit
   method final: clipboard -> unit
end ;;

Then programs became the construction of object like the following:

let copy_file_cmd_name_args src dst =
   let src_cmd = open_file_cmd src in
   let dst_cmd = open_file_cmd dst in
     seq_execute_cmd [
        par_execute_cmd [src_cmd, dst_cmd],
        copy_file_cmd_fd_func_args src_cmd#file dst_cmd#file
     ];

Here serial_execute runs through the commands executing them one by one 
until either it finishes or an error is generated in which case it backs 
out by calling undo. Finally objects are given a chance to release 
resources when final is called.

This way we were sure that resources would be properly accounted for. Of 
course hardware faults or mis-behaving clients can still cause you 
trouble. Particularly tracing which operation first caused a failure and 
what conditions lead to that failure.

It seems to me that if you have to catch errors then you have to 
construct some similar framework in Ocaml. There's no language support 
for writing commands.

regards,

Richard.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      parent reply	other threads:[~2004-07-11 14:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20040628143917.GA21847@fichte.ai.univie.ac.at>
     [not found] ` <Pine.LNX.4.44.0406290056580.1229-100000@localhost>
     [not found]   ` <20040628173400.GB26193@fichte.ai.univie.ac.at>
2004-06-29  1:02     ` [Caml-list] " skaller
2004-07-04  7:30       ` Lauri Alanko
2004-07-04 20:16         ` Christophe TROESTLER
2004-07-04 20:24         ` Michael Hicks
2004-07-05  3:42           ` skaller
2004-07-11 13:12       ` Richard Cole [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='ccree9$l6$1@sea.gmane.org' \
    --to=rj-cole@iinet.net.au \
    --cc=caml-list@inria.fr \
    --cc=ocaml-lib-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox