caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: Thomas Fischbacher <Thomas.Fischbacher@Physik.Uni-Muenchen.DE>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to raise_with_arg() a tuple?
Date: Thu, 7 Jul 2005 20:42:04 +0200	[thread overview]
Message-ID: <17101.30588.827486.654309@karryall.dnsalias.org> (raw)
In-Reply-To: <Pine.LNX.4.61.0507071949030.20672@katrin.cip.physik.uni-muenchen.de>

 Thomas Fischbacher [Thursday 7 July 2005] :
 >
 > 
 > Hello together,
 > 
 > I'd like to throw an exception from within C code which provides
 > more information to Caml than just a string.
 > 
 > If I do it that way:
 > 
 > === Caml ===
 > 
 > exception Test_exn of int * int

It's the usual gotcha with variant constructor that have multiple
arguments. The exception you defines has two arguments, whereas 

 exception Test_exn of (int * int)

has only one argument -- a tuple. It's not the same thing.

 > external test_raise_tuple: bool -> (int*int) = "caml_test_raise_tuple"
 > 
 > let _ = Callback.register_exception "ocaml_exn_test" (Test_exn (0,0));;
 > 
 > === C ===
 > 
 > CAMLprim caml_test_raise_tuple(value x)
 > {
 >   CAMLparam1(x);
 >   int cx;
 > 
 >   cx=Int_val(x);
 >   fprintf(stderr,"DDD x=%d\n",cx);
 > 
 >   CAMLlocal1(ex);
 >   ex=alloc_tuple(2);
 > 
 >   Store_field(ex,0,Val_int(2));
 >   Store_field(ex,1,Val_int(4));
 > 
 >   if(cx)
 >     {
 >       raise_with_arg(*caml_named_value("ocaml_exn_test"), ex);

here you are raising an exception with one tuple argument, this
doesn't match the exception you defined.

There's no function to directly create an exception value with
multiple argument, you have to do this (IIRC, not tested) :

  ex = alloc(3, 0);
  Store_field(ex, 0, *caml_named_value("ocaml_exn_test");
  Store_field(ex, 1, Val_int(2));
  Store_field(ex, 2, Val_int(4));
  caml_raise(ex);

 > ===>
 >  Your function may raise an exception or return a [value] with the
 >  [CAMLreturn] macro.  Its argument is simply the [value] returned by
 >  your function.  Do NOT directly return a [value] with the [return]
 >  keyword.  If your function returns void, use [CAMLreturn0].
 > <===

actually it's ok to raise exception directly (ie, not through
CAMLreturn), the runtime takes care of releasing the local GC roots.

-- 
   Olivier


  reply	other threads:[~2005-07-07 18:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-07 18:05 Thomas Fischbacher
2005-07-07 18:42 ` Olivier Andrieu [this message]
2005-07-08 15:55   ` [Caml-list] " Thomas Fischbacher
2005-07-08 16:24     ` Kenneth Knowles
2005-07-08 16:58       ` Thomas Fischbacher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17101.30588.827486.654309@karryall.dnsalias.org \
    --to=andrieu@ijm.jussieu.fr \
    --cc=Thomas.Fischbacher@Physik.Uni-Muenchen.DE \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).