This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: How to use scm_internal_catch?


Szekeres Istvan <szekeres@cyberspace.mht.bme.hu> writes:

> I want to catch scm errors with a c function to override the default
> print-error-message-and-exit behaviour. I found out that scm_internal_catch
> is used for this, but I can't figure out how. Can somebody please write me a
> very short example program that 
>  - sets up an error handler
>  - tries to evaluate a "buggy" scheme script
>  - prints the error BUT doesn't exit
>  
> I'm not on the mailing list, please reply me directly.

You can use scm_handle_by_message_noexit as the handler for
scm_internal_catch to print a brief error message.  Here is a short
example:

    #include <libguile.h>

    SCM
    body (void *data)
    {
      /* do something bad here */
      return scm_quotient (SCM_MAKINUM(4), SCM_MAKINUM(0));
    }

    SCM
    handler (void *data, SCM tag, SCM throw_args)
    {
      /* produce a brief message */
      scm_handle_by_message_noexit ("foo", tag, throw_args);
      /* say that we have been here */
      fprintf (stderr, "error caught.\n");
      return SCM_BOOL_F;
    }

    void
    inner_main (void *data, int argc, char **argv)
    {
      SCM result;

      result = scm_internal_catch (SCM_BOOL_T,
				   body, NULL,
				   handler, NULL);
      printf ("result is: ");
      scm_display (result, SCM_UNDEFINED);
      printf ("\n");
    }

    int
    main (int argc, char **argv)
    {
      scm_boot_guile (argc, argv, inner_main, NULL);
      return 0;
    }