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
Line number for index out of bounds
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-03-06 (19:09)
From: Andries Hekstra <andries.hekstra@p...>
Subject: Re: [Caml-list] Line number for index out of bounds
Dear Richard,

Thanks for your email. I indeed use native code as I need the speed. My 
program is 3500 lines, and includes multi-dimensional arrays, to putting 
try's everywhere by hand is out of the question. I would then have to 
write a metaprogram that adds such try commands to an existing OCaml 
program and outputs a longer program with the try's with the asserts. If 
possible I would like to postpone that and try your other option. 

Due to this crashing business I go on a business trip to Asia without any 
ready simulation results for one week.

> * Use bytecode, and before running the program set the environment
> variable OCAMLRUNPARAM=b which will print a stack trace.

If I would use this week of the trip to try this suggestion you made, how 
will the stack trace give me the line number?

Best regards,


Dr. Ir. Andries P. Hekstra
Philips Research 
High Tech Campus 27  (WL-1-4.15)
5656 AG Eindhoven
Tel./Fax/Secr. +31 40 27 42048/42566/44051 
   *  Good open source break software for computer users : 

Richard Jones <rich@annexia.org> 
06-03-2006 12:14

Re: [Caml-list] Line number for index out of bounds

On Mon, Mar 06, 2006 at 11:44:31AM +0100, Andries Hekstra wrote:
> Invalid_argument("index out of bounds")
> Of course, I am very curious in which line number of the program this 
> exception occurs. 
> Is there any way to get hold of this line number?

This is a real problem with OCaml - it's impossible to get stack
traces of where an exception happens with native code.  I'm assuming
you're using native code.  I commonly have cases where a program dies
with "exception: Not_found" because I forgot to enclose some List.find
with an appropriate try ... with clause, or made some wrong
assumption.  Tracking these down is time-consuming.

Possible workarounds:

* Use bytecode, and before running the program set the environment
variable OCAMLRUNPARAM=b which will print a stack trace.

* Surround every possible array index with a try ... with expression
like this:

    (* code which accesses the array *)
    Invalid_argument "index out of bounds" -> assert false

The "assert false" will print the line and character number of the

* Hack ocamlopt to be able to print exceptions properly :-)


Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business -