caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Catching errors (Unix-Module)
@ 2002-04-15 20:51 Oliver Bandel
  2002-04-15 22:59 ` Shawn Wagner
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Bandel @ 2002-04-15 20:51 UTC (permalink / raw)
  To: caml-list

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)
#
=============================================

I looked into the reference-Handbook and found that
the unix-error is of type

  error * string * string


So, when i look into the line 
 "Uncaught exception: Unix.Unix_error (Unix.ENOENT, "stat", "/foo/bar")."

the part "(Unix.ENOENT, "stat", "/foo/bar")" looks like
the error*string*string.

But how to handle this?

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.

What about the type of the error?
I understand how normal functions have to be designed
in respect to their types.

But I don't understand, how exceptions will be handled
in respect of their types.
Some days ago I tried some simple examples and thought
that I had understand the exception-system of OCaml,
but now, when doing "real word"-programming,
I realize that I didn't understand it.


So, please help.


TIA,
  Oliver

P.S.: When defining an exception that already exists (Division_by_zero for
      example), the exception can't be catched with try (at least this problem
      occured with OCaml 3-.01 on Linux).


-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Catching errors (Unix-Module)
  2002-04-15 20:51 [Caml-list] Catching errors (Unix-Module) Oliver Bandel
@ 2002-04-15 22:59 ` Shawn Wagner
  2002-04-16 13:04   ` Oliver Bandel
  0 siblings, 1 reply; 5+ messages in thread
From: Shawn Wagner @ 2002-04-15 22:59 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Catching errors (Unix-Module)
  2002-04-15 22:59 ` Shawn Wagner
@ 2002-04-16 13:04   ` Oliver Bandel
  2002-04-16 13:22     ` Remi VANICAT
  2002-04-16 13:56     ` Hendrik Tews
  0 siblings, 2 replies; 5+ messages in thread
From: Oliver Bandel @ 2002-04-16 13:04 UTC (permalink / raw)
  To: Shawn Wagner; +Cc: caml-list

Hello,

On Mon, 15 Apr 2002, Shawn Wagner wrote:

> 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)


OK, let's try it:
============================================
oliver@first:/home/oliver > unix-top
        Objective Caml version 3.01

# 
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)

                ;;
Characters 74-161:
This expression has type unit but is here used with type Unix.stats
# 
============================================


Maybe this is a OCaml 3.01-problem?

Or is there a general problem with that code?


Ciao,
   Oliver

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Catching errors (Unix-Module)
  2002-04-16 13:04   ` Oliver Bandel
@ 2002-04-16 13:22     ` Remi VANICAT
  2002-04-16 13:56     ` Hendrik Tews
  1 sibling, 0 replies; 5+ messages in thread
From: Remi VANICAT @ 2002-04-16 13:22 UTC (permalink / raw)
  To: caml-list

Oliver Bandel <oliver@first.in-berlin.de> writes:

> Hello,
> 
> On Mon, 15 Apr 2002, Shawn Wagner wrote:
> 
> > 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)
> 
> 
> OK, let's try it:
> ============================================
> oliver@first:/home/oliver > unix-top
>         Objective Caml version 3.01
> 
> # 
> 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)
> 
>                 ;;
> Characters 74-161:
> This expression has type unit but is here used with type Unix.stats

Your expression have a type, an which ever case happen, it must return
a value of the same type (here Unix.stats), then Printf.fprintf
doesn't return something of type Unix.stats, an so this code doesn't
work.


Something like :
try
  Some (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);
   None

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

(but in the latter case, the exception is reraised after the message
have been printed, it may not be what you want).



-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Catching errors (Unix-Module)
  2002-04-16 13:04   ` Oliver Bandel
  2002-04-16 13:22     ` Remi VANICAT
@ 2002-04-16 13:56     ` Hendrik Tews
  1 sibling, 0 replies; 5+ messages in thread
From: Hendrik Tews @ 2002-04-16 13:56 UTC (permalink / raw)
  To: Oliver Bandel, caml-list

Oliver Bandel writes:
   Date: Tue, 16 Apr 2002 15:04:34 +0200 (MET DST)
   Subject: Re: [Caml-list] Catching errors (Unix-Module)
   
   OK, let's try it:
     [...]   

   This expression has type unit but is here used with type Unix.stats

   Or is there a general problem with that code?
   
Well, the catch has to return something compatible with
Unix.stat. So you can try

1. use optional values 

    try
     Some (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);
      None
    ;;

2. reraise the exception

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

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


In case you get an error like 

      Reference to undefined global `Unix'

you have to built a toplevel containing the unix module:

  /usr/local/ocaml-3.01/bin/ocamlmktop -o mytop unix.cma


Bye,

Hendrik
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-04-16 13:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-15 20:51 [Caml-list] Catching errors (Unix-Module) Oliver Bandel
2002-04-15 22:59 ` Shawn Wagner
2002-04-16 13:04   ` Oliver Bandel
2002-04-16 13:22     ` Remi VANICAT
2002-04-16 13:56     ` Hendrik Tews

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).