Catching C errors

data pulverizer data.pulverizer at gmail.com
Thu Oct 20 09:52:05 UTC 2022


On Wednesday, 19 October 2022 at 16:48:10 UTC, Ali Çehreli wrote:
> On 10/19/22 07:05, data pulverizer wrote:
>
> > I am calling code from a C API, and would like to know how to
> catch exit
> > errors
>
> If you are talking about the exit() Posix function, you can't 
> do anything about that because its purpose is to cause "normal 
> process termination".
>
> Ali

Yes it is the `exit()` function in the end I figured that it 
couldn't be sidestepped like an exception so I did something else.

As it happens, the situation turned out to be a bit trivial, not 
even sure if it's worth going into. But for those interested the 
description is below.

I'm currently writing a D interop with R, the dynamic statistical 
programming language. There's a function called 
`Rf_initEmbeddedR()` which allows you to call the full R C API 
from D without having to compile a DLL and call code from R. 
There is also a function called `Rf_endEmbeddedR(int fatal)`, 
which I assumed terminates the R session, but it doesn't, after 
seeing the code it only cleans some things up and looks as if it 
is intended to be used just before you exit main.


I have unit tests in D that begin with the init function and 
finish with the end function, and when I tried re-initialising I 
get an exit() error saying the R session was already initialized. 
So all I did was create a static init flag, and a wrapper 
function to only call init if the flag is false.

As I said in the end it was trivial.


More information about the Digitalmars-d-learn mailing list