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
try with
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-05 (15:49)
From: Dave Benjamin <dave@r...>
Subject: Re: [Caml-list] try with
On Sun, 5 Aug 2007, tmp123@menta.net wrote:

> Sorry for an easy question about exceptions:
> If in a code like:
> let v1 = f1 () in
> f2 v1
> I like to handle in a different way an exception (like Not_found) 
> produced inside f1 of the same one produced in f2 (or even not treat the 
> one produced in f2), which one is a correct way to write the code?

One way (besides the exception technique you posted) is to convert the 
exception to an option or other variant:

let maybe_v1 = try Some (f1 ()) with Not_found -> None in
match maybe_v1 with
   | Some v1 -> f2 v1
   | None -> handle_f1_exception ()

Also see the following page for a "let try" syntax extension: