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
[Caml-list] Catching exceptions into strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-06-12 (15:27)
From: Xavier Leroy <xleroy@p...>
Subject: Re: [Caml-list] Catching exceptions into strings
> Hmm.  I didn't think it was possible to catch floating point exceptions in
> ocaml (something I'd very much like to be able to do).  Why doesn't ocaml
> support this?  Or does it?

Your question requires a bit of decoding.

Floating-point operations in OCaml follow the IEEE-standard usage of
never causing an exception, but return infinities or (silent) NaNs
when something is weird.

The "Floating point exception" you saw in Gaurav's message is a
misnomer: *integer* division by zero generates a SIGFPE signal
under Unix, which is printed as "Floating-point exception" but
really means "Integer arithmetic exception".

Most processors (and I believe the IEEE standard) offer the option
to raise exceptions instead of returning NaNs, infinities or denormals.
This facility is not available from OCaml because it's not provided
by ANSI C nor by POSIX, which are essentially what the OCaml runtime
system is building upon.

Even then, you'd get a SIGFPE when a floating point operation goes bad,
and we're back to the issue of mapping a Unix *synchronous* signal to
a Caml exception.  (Asynchronous signals are already dealt with.)
It can be done, although not trivially.  But the main issue here is to
get enough context about the signal.  Raising Division_by_zero every
time SIGFPE is received is not correct: maybe someone did "kill -FPE <pid>"
from the outside; maybe the SIGFPE was generated inside a C library
linked with the Caml code, and which has its own rules for dealing
with it.

In other terms, what is needed is to collect enough context on the
SIGFPE occurrence to 1) make sure it's an integer division operation
that failed, and 2) make sure this happened in OCaml code; only then
we could generate a Division_by_zero exception.  But of course
there is no portable way to do all this under Unix.

In summary: you can get SIGFPE, but they can only stop your program,
handling them as exceptions is just not possible.

- Xavier Leroy
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners