[plt-scheme] Code for catching exceptions in embedding applications

From: Sergey Khorev (sergey.khorev at gmail.com)
Date: Mon Nov 3 03:14:07 EST 2008

>> Code below (basically it has been extracted from PLT documentation)
>> crashes with
>> #f::0: compile: bad syntax; function application is not allowed, because
>> no #%app syntax transformer is bound in: (lambda (thunk) (with-handlers
>> ((void (lambda(exn) (cons #f exn)))) (cons #t (thunk))))
>> Seg fault (internal error) at 4
>> Do I need to `require' some module for this to work?
> Yes. The initial namespace is empty; it doesn't even have syntax for
> `lambda' or for function application. So, you need to require something
> like `scheme/base'.

Ok, I created base.c with mzc --c-mods base.c ++lib scheme/base
but unable to require it into new namespace (getting scheme/base:
standard-module-name-resolver: collection not found: "scheme" in any
of: () in: scheme/base), please see code below. Do I need to populate
MzScheme collection path? What is the point in use of base.c then?

#define MZ_PRECISE_GC 1

#include <scheme.h>

#include "base.c"

static Scheme_Env *environment = NULL;

    static int
scheme_main(void *data)
    char *e =
	"(lambda (thunk) "
	"(with-handlers ([void (lambda (exn) (cons #f exn))]) "
	"(cons #t (thunk))))";

    Scheme_Env *env = NULL;
    Scheme_Object *exn_catching_apply = NULL;
    Scheme_Object *sym = NULL;

    MZ_GC_VAR_IN_REG(0, env);
    MZ_GC_VAR_IN_REG(1, exn_catching_apply);
    MZ_GC_VAR_IN_REG(2, sym);


    environment = scheme_basic_env();
    sym = scheme_intern_symbol("scheme/base");

    env = (Scheme_Env *)scheme_make_namespace(0, NULL);
    scheme_set_param(scheme_current_config(), MZCONFIG_ENV, env);
    exn_catching_apply = scheme_eval_string(e, env);


    return 1;

int main()
    return scheme_main_stack_setup(1, scheme_main, NULL);

Posted on the users mailing list.