English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2007-02-02 (01:19)
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 () =
	    unlink "no_such"
	    | 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

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?