Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Catching errors (Unix-Module)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Shawn Wagner <shawnw@s...>
Subject: Re: [Caml-list] Catching errors (Unix-Module)
On Mon, Apr 15, 2002 at 10:51:41PM +0200, Oliver Bandel wrote:
> Hello,
> 
> I'm experimtnzing with the Unix-module and try to
> cath Unix-errors.
> 
> Look here:
> 
> 
> =============================================
> oliver@first:/home/oliver > unix-top 
>         Objective Caml version 3.01
> 
> # Unix.stat "/foo/bar";;
> Uncaught exception: Unix.Unix_error (Unix.ENOENT, "stat", "/foo/bar").
> # 
> =============================================
> 
> How to handle such Errors?
> 
> I tried this one:
> 
> =============================================
> # try Unix.stat "/foo/bar" with Unix.Unix_error -> "shi...";;
> Characters 30-45:
> The constructor Unix.Unix_error expects 3 argument(s),
> but is here applied to 0 argument(s)
> #
> =============================================

try
 Unix.stat "/foo/bar"
with
| Unix.Unix_error (code, func, file) ->
  Printf.fprintf stderr "Couldn't %s '%s': %s!\n"
    func file (Unix.error_message code)

You have to include all of the type constructor's arguments in an exception
handler pattern (Using _ if you don't care about the actual value, i.e.
Unix.Unix_error(code, _, _).

> 
> What actions can I use here?
> I may want to ignore some of those errors under some conditions,
> but I also may want to handle them.

You can catch any Unix_error with the above, or you can catch specific ones
you're interested in like so:

try
 blah
with
| Unix.Unix_error(Unix.ENOENT, func, arg) -> whatever
| Unix.Unix_error(Unix.EACCES, func, arg) -> another error handler
| Unix.Unix_error(code, func, arg) -> general Unix_error handler

-- 
Shawn Wagner
shawnw@speakeasy.org
-------------------
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