Version française
Home     About     Download     Resources     Contact us    
Browse thread
Exception Unix_error problem in toplevel
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Bob Williams <a6a37331@t...>
Subject: Exception Unix_error problem in toplevel
Summary of problem: If you call a function from a script, that
function will raise and catch Unix_error properly.  However, if you
call the same function interactively from an OCaml toplevel, the
function will see an exception but will think it is *not* Unix_error.
A demonstration appears below.


OCaml version :  3.09.2-9
Distribution  :  Debian Linux for i386
Linux kernel  :  2.6.18

Here is file "bug.ml":

	open Unix
	open Printf
	
	let delete_file () =
	  try
	    unlink "no_such"
	  with
	    | Unix_error (_, _, _) -> Printf.eprintf "Unix error.\n%!"
	    | _                    -> Printf.eprintf "Some other error.\n%!"

Here is the script "run.ml":

	Bug.delete_file ()

The following works properly:

	$ ocamlc -c bug.ml 
	$ ocaml unix.cma bug.cmo run.ml
	Unix error.

The output "Unix error" is correct because the file named "no_such"
does not exist.  (By the way, "unix.cma" is OCaml's standard Unix
library.)

Now I omit the script "run.ml" from the ocaml command line and call
Bug.delete_file interactively, as follows:

	$ ocaml unix.cma bug.cmo
	        Objective Caml version 3.09.2
	
	# Bug.delete_file ();;
	Some other error.

The output should be the same as for the first example, but it isn't.
Strange, isn't it?