* Re: [Caml-list] try with
2007-08-05 15:29 try with tmp123
@ 2007-08-05 15:23 ` Dave Benjamin
2007-08-05 15:50 ` skaller
1 sibling, 0 replies; 3+ messages in thread
From: Dave Benjamin @ 2007-08-05 15:23 UTC (permalink / raw)
To: tmp123; +Cc: caml-list
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:
http://martin.jambon.free.fr/extend-ocaml-syntax.html#lettry
^ permalink raw reply [flat|nested] 3+ messages in thread
* try with
@ 2007-08-05 15:29 tmp123
2007-08-05 15:23 ` [Caml-list] " Dave Benjamin
2007-08-05 15:50 ` skaller
0 siblings, 2 replies; 3+ messages in thread
From: tmp123 @ 2007-08-05 15:29 UTC (permalink / raw)
To: caml-list
Hello,
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?
Because the only thing I see up to now is something so overloaded as:
exception Skip;
try
let v1 =
try
f1()
with
Not_found -> handle_f1_exception();
raise Skip
in
f2 v1
with
Skip -> ();
Thanks again.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] try with
2007-08-05 15:29 try with tmp123
2007-08-05 15:23 ` [Caml-list] " Dave Benjamin
@ 2007-08-05 15:50 ` skaller
1 sibling, 0 replies; 3+ messages in thread
From: skaller @ 2007-08-05 15:50 UTC (permalink / raw)
To: tmp123; +Cc: caml-list
On Sun, 2007-08-05 at 17:29 +0200, tmp123@menta.net wrote:
> Hello,
>
> 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?
Eliminate the exceptions, they're usually a bad idea:
let v1 = try Some (f1 ()) with Not_found -> None in
let v2 = match v1 with
| None -> Error1
| Some x -> try Some (f2 x) with Not_found -> None
in match v2 with
| None -> Error2
| Some v2 -> ...
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-08-05 15:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-05 15:29 try with tmp123
2007-08-05 15:23 ` [Caml-list] " Dave Benjamin
2007-08-05 15:50 ` skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox