Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: Michel Pastor <K4ng@codefx.org>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Infinite loop when catching an exception
Date: Fri, 10 Dec 2004 09:21:49 +0100	[thread overview]
Message-ID: <16825.23709.530721.745815@gargle.gargle.HOWL> (raw)
In-Reply-To: <41B91A9F.8050400@codefx.org>


Michel Pastor writes:
 > 
 > I have a problem with a little piece of code but
 > I attached the whole thing since i cannot reproduce
 > the "bug" in reduced environement with the same values.
 > I'm using the num library for doing exact maths.
 > 
 > [...]
 >
 > If I try to catch the exception like that the process
 > wont even go beyond the "try with" block
 > I tested printing something instead of trying to exit
 > but nothing is writed to the console and the cpu is at 100%
 > All I can do is to kill the process.
 > But if I don't try catch the Failure all works fine.
 > I don't think doing unsafe things since there is no C code,
 > no black magic, only some maths.

This is  a ``syntax'' problem: you  are not parsing the  code the same
way the ocaml compiler does. When you insert the try/with like this:

 > (************************)
 > (*  HERE IS THE PROBLEM *)
 > (************************)
 > 
 > let draw_curve color =
 >   begin
 >     set_color color;
 >     let index = ref !x_min in
 >     let ival = (!x_max -/ !x_min) // num_of_int (graph_width * 2) in
 >     while !index <=/ !x_max do
 >       let yval = poly_apply poly !index in
 >      (* uncomment the "try with" block to trigger the bug *)
 >      try 
 >      begin
 > 	print_endline ("ici " ^ string_of_num yval);
 > 	let (graph_x, graph_y) = scale_point (!index, yval) in
 > 	  begin
 > 	    printf "%d %d\n" graph_x graph_y;
 > 	    fill_circle graph_x graph_y 1;
 > 	  end;
 > 	print_endline "la";
 > 	end;
 >      with _ -> failwith "ici maintenant";
 >       print_endline "et encore la";
 >       index := !index +/ ival;
 >     done
 >   end

then the three  lines "failwith ...; print_endline ...;  index := ..."
are  all  part  of the  exception  handler.   Since  in your  code  no
exception is  raised, these lines  are never evaluated,  including the
update of the index, which explains the infinite loop.

If you really  need this try/with, you need to  rewrite your code this
way (for instance):

	while ... do
	  ...
	  begin 
	    try
	      ...
	    with _ ->
              ...
	  end;
	  ...
        done

To  avoid  such pitfalls,  it  is convenient  to  use  an editor  with
automatic indentation of  the code which is consistent  with the ocaml
syntax, such as the Tuareg mode under Emacs. At some point, you become
so  familiar with ocaml  syntax that  you don't  need it  anymore, but
meanwhile it helps.

Best regards,
-- 
Jean-Christophe


  reply	other threads:[~2004-12-10  8:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-10  3:40 Michel Pastor
2004-12-10  8:21 ` Jean-Christophe Filliatre [this message]
2004-12-10  8:51   ` [Caml-list] " skaller
2004-12-10 10:32     ` Radu Grigore
2004-12-10 13:49   ` Michel Pastor

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=16825.23709.530721.745815@gargle.gargle.HOWL \
    --to=jean-christophe.filliatre@lri.fr \
    --cc=K4ng@codefx.org \
    --cc=caml-list@yquem.inria.fr \
    /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