Re: general purpose C++ exception handler in lisp

Grégory Vanuxem <[email protected]> Wed, 12 Feb 2025 10:07:20 +0100
Newsgroups gmane.lisp.steel-bank.general
Message-ID <CAHnU2daXWgmNtt3nO_9TgxJkNEp2z1B--7MGtuUSoVooUD-xMA@mail.gmail.com>
Hi Andrew,

I respond to you question because I use libjulia.* (julialang.org) with
"does-not-catch-exceptions/errors-let-my-underlying-lisp-catch-an-handle-them".
And to add more uncertainty and to start with, I am not a real CL coder I
do what I can. Do you mean something like:

(defun divide (numerator denominator)
  (handler-case
      (/ numerator denominator)
    (division-by-zero ()
      (format t "Error: Division by zero is not allowed."))))

(Copilot generated, do not burn me please ;)) No tested code frankly but I
guess the important keyword for you is 'handler-case'. The SBCL source code
has easily finding files(s) that handle floating point exceptions to handle
them. This is another subject, more linked to the material, but that can
give you some hints).

- Greg

Le sam. 8 févr. 2025 à 21:49, Andrew Wolven <[email protected]> a écrit :

> I'm working with some rather large C++ libraries.
>
> In the past, I have written C wrappers for C++ calls, which inside the
> wrapper, uses a C++ try/catch form which catches any exception (...) and
> calls a lisp callback function which signals a lisp error.
>
> It would behoove my build process and [possibly] reduce complexity if I
> could get rid of the C wrappers and call the C++ methods directly from Lisp
> with the ffi.
>
> The problem is, these C++ methods sometimes throw exceptions.  It would be
> really cool if I had a macro, like 'with-try/catch' where I could set up a
> C++ exception handler frame, and execute the body of the macro, which is
> lisp code, but most likely contains a foreign function call to a C++ method
> which can throw.
>
> The catch portion could translate the C++ exception into a lisp condition
> and signal the lisp condition.  I don't think I would rethrow if a
> translation is not found, but rather signal a general C++-error lisp
> condition.
>
> I've seen some posts which indicate that something like this may be doable
> on Windows (windows amd64 is my primary platform), but it would also be
> good if it worked on Linux and Macos.
>
> Does anyone have thoughts on how I could implement such a macro?
>
> Thanks,
> Andrew Wolven
> _______________________________________________
> Sbcl-help mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sbcl-help
>

_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help