[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2004-12-10 (08:21) |
From: | Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...> |
Subject: | Re: [Caml-list] Infinite loop when catching an exception |
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