Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: small code problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: small code problem
> I'm having a problem with a core dump due to an uncaught exception.
> The following snippet of code best illustrates the problem:

[snip]

> 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, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl