chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] throwing exception from C


From: John Lenz
Subject: [Chicken-users] throwing exception from C
Date: Thu, 17 Mar 2005 07:52:52 +0000

I am adding exception handling to SWIG (I committed some code into CVS earlier today) and I am using the following code to throw an exception from C. It seems to be working in my testing, but I just want to confirm that everything will be ok from the chicken and the stack side of things.

static void SWIG_Chicken_ThrowException(C_word value) C_noret;
static void SWIG_Chicken_ThrowException(C_word value)
{
  char *aborthook = C_text("\003sysabort");
  C_word *a = C_alloc(C_SIZEOF_STRING(strlen(aborthook)));
  C_word abort = C_intern2(&a, aborthook);

  abort = C_block_item(abort, 0);
  if (C_immediatep(abort))
    SWIG_Chicken_Panic(C_text("`##sys#abort' is not defiend"));

  C_save(value);
  C_do_apply(1, abort, C_SCHEME_UNDEFINED);
}

SWIG_Chicken_Panic just calls C_halt with the right arguments.

And then when SWIG sees a declaration like
int func() throws (int, Foo *);
in the SWIG input file, it will generate something like

static void _wrap_func(C_word argc, and so on) {
//whatever... check types of args, etc.
try {
  res = C_fix(func(C_unfix(scm1)));
} catch (int a) {
  SWIG_Chicken_ThrowException(C_fix(a));
} catch (Foo *f) {
  // create C_mswigpointer call ThrowException
}

C_kontinue(continuation, res);

}

I am then catching these with (handle-exceptions)

Is this the right way to throw an exception from C code? My only concern is that we are calling C_do_apply from inside the catch { } block, so when throwing an exception the catch { is still left on the stack which might impact future throws later on the stack. We don't want C++ unrolling the stack all the way back up to this function if another exception is thrown. Perhaps it won't, since an exception thrown from inside a catch block propagates up to it's parent. Secondly, C++ exceptions do not propagate across C code (at least for gcc), unless the C code is compiled with -fexceptions, in which case the chicken runtime might "protect" us from exceptions going every which way.

John





reply via email to

[Prev in Thread] Current Thread [Next in Thread]