Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Seeking exception source
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-08 (02:13)
From: skaller <skaller@u...>
Subject: Seeking exception source
I am occasionally annoyed by Not_found propagating to my
top level function .. meaning the error could be anywhere
at all in my program.

I usually do this rubbish to fix it:

 ...... e ...... (* where e might raise an exception *)


.... begin try e 
 with Not_found -> failwith "Not_found at So and So" end ...

and then litter the code with such things until I've found
where the problem is. This is very bad.

A better way? Something like this:

let f x ... = ....


let f x ... = begin try
with (x:exn) -> wrap "in function f" x end


let wrap s x =  raise (TaggedExn ( s,x))

The idea is that every exception continues to propagate
as normal .. but it gets a tag of where it came from
wrapped around it. The handler can then report it.

This mechanism is no use in a recursive function, nor where
some exception can legitimately escape .. however both these
could be taken into account: the general idea is to add
a symbolic stack backtrace to exceptions which represent
errors that WILL propagate to the top level (ie. underflow
the stack).

Any ideas how to do that? Perhaps a camlp4 thing to instrument
marked functions? Although it isn't clear catching exceptions
costs anything if none are thrown (other than code bloat)?

[Actually in C the macro __LINE__ solves this .. but caml
doesn't have such macros ..]

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: