Re: small code problem

From: Markus Mottl (
Date: Thu Jul 08 1999 - 06:56:21 MET DST

From: Markus Mottl <>
Message-Id: <>
Subject: Re: small code problem
Date: Thu, 8 Jul 1999 05:56:21 +0100 (MET DST)
In-Reply-To: <> from "" at Jul 2, 99 03:30:34 pm

> I'm having a problem with a core dump due to an uncaught exception.
> The following snippet of code best illustrates the problem:


> Why do the two compilers exhibit different behaviors with the same code?

I'd say that this is an incompatibility bug between the byte- and native
code compiler:

Both actually throw an exception - it's just not the same!

You didn't write which version of OCaml you use. The version I use
(ocaml-2.02-2) does not produce a native code executable that crashes. It
emits an uncaught exception to stderr instead.

Try this very short file:

  let main () = Sys.argv.(1)
  let _ = Printexc.catch main ()

This should guarantee that any uncaught exceptions get printed to stderr
instead of producing a core dump.

Then you will see that this file compiled to byte code (ocamlc) produces
this output:

  Uncaught exception: Invalid_argument("Array.get")

Whereas the native code version (ocamlopt) produces:

  Uncaught exception: Invalid_argument("out-of-bound array or string access")

The interpreter would behave like the byte code version - if we didn't
have the second incompatibility that the argument vector behaves
differently as mentioned in another current thread in the mailing list.

Interesting that so far noone else has come across the incompatibility
of exceptions in this case...

> As an aside, I don't particular like my use of lazy and force. Is
> there a cleaner way do the same thing?

Yes, you only need to make "usage" a unit-function:

  let usage () = Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1
  let filename =
    try Sys.argv.(1) with Invalid_argument("Array.get") -> usage ()

Best regards,
Markus Mottl

Markus Mottl,,

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:23 MET