From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 4ED70BC48 for ; Sun, 3 Apr 2005 11:47:17 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j339lGEk013052 for ; Sun, 3 Apr 2005 11:47:17 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA23801 for ; Sun, 3 Apr 2005 11:47:16 +0200 (MET DST) Received: from smtp10.wanadoo.fr (smtp10.wanadoo.fr [193.252.22.21]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j339lGGf013049 for ; Sun, 3 Apr 2005 11:47:16 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf1007.wanadoo.fr (SMTP Server) with ESMTP id 33474280013D for ; Sun, 3 Apr 2005 11:47:16 +0200 (CEST) Received: from Galois (Mix-Strasbourg-209-2-208.w80-9.abo.wanadoo.fr [80.9.105.208]) by mwinf1007.wanadoo.fr (SMTP Server) with ESMTP id 8AE182800139 for ; Sun, 3 Apr 2005 11:47:15 +0200 (CEST) X-ME-UUID: 20050403094715569.8AE182800139@mwinf1007.wanadoo.fr Received: from evariste by Galois with local (Exim 4.50) id 1DI1gS-0000fE-Cb for caml-list@inria.fr; Sun, 03 Apr 2005 11:46:20 +0200 Date: Sun, 3 Apr 2005 11:46:20 +0200 From: =?iso-8859-1?Q?S=E9bastien?= Hinderer To: caml-list@inria.fr Subject: Raising an exception with several arguments from C Message-ID: <20050403094620.GA2436@galois> Mail-Followup-To: caml-list@inria.fr Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Miltered: at nez-perce with ID 424FBBA4.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 424FBBA4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ens-lyon:01 ocaml's:01 variants:01 caml's:01 cheers:01 exception:01 exception:01 unix:01 pair:01 pair:01 exceptions:01 exceptions:01 functions:01 int:01 int:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Dear all, In chapter 18 of Ocaml's manual, two functions are mentionned to raise exceptions from C: caml_raise_constant which raises a constant exception, and caml_raise_with_arg which raises an exception with one argument. However, assume the following declaration: exception E of int * string;; Then E is actually an exception with _two_ arguments, and not an exception with one pair argument. Consequently, caml_raise_with_arg can not be used to raise E from within a C function. There are two solutions to this problem: (1) declare E as follows : exception E of (int * string) Then E will be an exception with one argument (which is a pair), so it will be possible to raise E thanks to caml_raise_with_arg. (2) As can be seen in the sources of the Unix module, an exception with n arguments (n>=2) is stored as a block of size n+1 tagged with 0. The field 0 of this block contains the value describing the exception, as returned by the caml_named_value function. Fields 1 to n contain values corresponding to arguments 1 to n of E (this encoding comes from the fact that exceptions are variants, I guess?). Once this block (let's call it res) is allocated and properlz filled, E can be raised thanks to a call such as caml_raise(res); (caml_raise is declared in caml/fail.h). Unfortunately, caml_raise does not seem to be documented in Caml's manual. Moreover, as far as I know, nothing is said about raising exceptions with more than one argument. Perhaps this point could be clarified in future versions of the manual ? Cheers, Sébastien.