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
[Caml-list] C++ Throws
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-08-28 (08:17)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] C++ Throws
> I do find it amazing that C++ would be dumb enough to try to scaffold raw
> stack frames, instead of using some kind of dynamic link pointers to reach
> each frame.

There are indeed two "schools" of exception handling: one that unwind
stack frames one by one until an exception handler is found, and one
that maintains at run-time a chaining between exception handling
blocks on the stack, so that no stack searching is necessary when an
exception is thrown.

The first school is exemplified by C++, Modula-3, Java and C#; the
second school by Lisp, Caml and to some extent Prolog (if you view
backtracking as a generalization of exception handling).

A good description of the two approaches is the following paper:

  "A Single Intermediate Language That Supports Multiple Implementations
   of Exceptions",, Norman Ramsey and Simon Peyton Jones, PLDI 2000.

The two approaches have very different performance trade-offs.  To
make things worse, many people from the first school are not even
aware of the second approach.  So, as usual, there is no hope to see
the world converge on a single exception mechanism.

>  How in the world would any kind of cross-language
> interoperability ever function if this were the case. 

Cross--language exception interoperability is certainly a challenge.
There are C compilers (MSVC, Tru64 cc) that provide C++-style
exception handling compatible with C++, but that's not too hard given
that the same compilers double as C++ compilers :-)  Some ABI
(Application Binary Interfaces) specify the format of stack
descriptors and the unwinding algorithm, but not all.

The only portable way is to install catch-all exception handlers at
the boundaries between the two languages whose job is to convert
exceptions from one language into exceptions from the other language.
The Caml bytecode interpreter does something like this when
interoperating with C, using setjmp/longjmp to represent exceptions on
the C side.

For C++ to Caml callbacks, you can use callback_exn to invoke the Caml
code; it reifies whatever Caml exceptions can get out of the Caml code
as special return values, which the C++ wrapper can test and then
throw the appropriate C++ exception.

For the reverse direction (Caml calling C++), I'm afraid the only
solution is to use a C++ catch-all clause to turn C++ exceptions into
Caml exceptions.

Hope this clarifies the issue.

- Xavier Leroy

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: