caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rich@annexia.org>
To: caml-list@inria.fr
Subject: [Caml-list] Matching exceptions in C code
Date: Sat, 3 Jun 2017 07:44:31 +0100	[thread overview]
Message-ID: <20170603064431.GL28111@annexia.org> (raw)

I'm trying to catch a Unix_error exception in C code, and extract the
errno from it.  Getting the errno is fine, but the problem is matching
on the Unix.Unix_error exception.

My original code used the documented method:

  retv = caml_callbackN_exn (*cb, 4, args);

  if (Is_exception_result (retv)) {
    retv = Extract_exception (retv);
    if (Field (retv, 0) == *caml_named_value ("Unix.Unix_error"))
      ...

However the if statement never matched the exception.  (I verified on
the OCaml side that the correct exception is being thrown).

After reading:

  http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/097f63cfb39a80418f95c70c3c520aa8.en.html
  http://caml.inria.fr/pub/ml-archives/caml-list/2009/06/797e2f797f57b8ea2a2c0e431a2df312.en.html

it seems I'm not the first person to have this problem.  It may be
that Unix is linked twice, but I'm not really sure about that.  I'm
using -output-obj with unix.cmxa linked into the object file, and also
-lunix in the link step, which is the documented way to do things, but
I don't know if that means the Unix module initializes itself twice.

Anyway, I modified the code to this, which *works* and is actually
more convenient than the documented method (the real code matches on
several other system exceptions as well):

    ...
    retv = Extract_exception (retv);
    exn_name = String_val (Field (Field (retv, 0), 0));
    if (strcmp (exn_name, "Unix.Unix_error") == 0) {
      int errcode = code_of_unix_error (Field (retv, 1));

The questions are:

(a) Will this continue to work in future?  It seems to be
undocumented.

(b) Could we have some sort of official function to hide the details?

Rich.

             reply	other threads:[~2017-06-03  6:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-03  6:44 Richard W.M. Jones [this message]
2017-06-21 15:39 ` Richard W.M. Jones
2017-06-21 15:50   ` John Whitington
2017-06-21 16:06     ` Richard W.M. Jones

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=20170603064431.GL28111@annexia.org \
    --to=rich@annexia.org \
    --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).