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 Failure and failwith
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-06-21 (09:30)
From: Julien Signoles <Julien.Signoles@l...>
Subject: Re: [Caml-list] exception Failure and failwith

> It's all about the failwith function of pervasives (that simply raises
> an exception Failure of string).
> Normally, that exception can be cought by means of a try...with using as
> clause something like
> | Failure s -> .....
> But what if I want to catch the Failure exception? :-/
> It seems that no module defines it, so I did not find any way to
> identify the exception.

You're right: no module defines Failure. But you can make an alias for the
primitive Failure and use it:

exception Prim_Failure = Failure
exception Failure
let f () = failwith ""
let () =
  try f (); assert false
  with Prim_Failure _ -> print_endline "primitive failure"
     | Failure        -> print_endline "redefined failure"

Hope this helps,
Julien Signoles
mailto:Julien.Signoles@lri.fr ; http://www.lri.fr/~signoles
"In theory, practice and theory are the same,
but in practice they are different" (Larry McVoy)