Actually i think I was not clear about what I need. How would I apply Err_trap for a higher level function? For example, a call to Indexer_new() might fail under some circunstances and the program will exit due to that, I would like for it not to exit immediately (for example, to be able to do something else if the call fails). indexer_new() does nto accept a context and it returns something when it works.
Again, i might just be missing something obvious. Em seg, 30 de mar de 2015 às 14:30, Nick Wellnhofer <[email protected]> escreveu: > On 30/03/2015 14:51, Bruno Albuquerque wrote: > > I was checking the exceptions handling code for the C bindings and it > looks > > like it simply exists when an error happens. > > > > For my purposes, it would be better if I could somehow trap errors and > act > > upon them instead of simply exiting. Is there a way to do it at all? I > > noticed there is an Err_trap function in Clownfish but I am not sure if > > that would do what I want or even how to use it. > > Yes, the Err_trap function is what you want to use. It takes a function > pointer and a void pointer to a user context and executes the function. If > a > Clownfish error is thrown, it is returned. Example: > > static void > try_something(void *context) { > /* Execute code that might throw. */ > } > > void > other_code() { > Err *error = Err_trap(try_something, context); > if (error != NULL) { > /* Handle error. */ > } > } > > The problem with this approach is that it's based on setjmp/longjmp and can > leak memory, so our goal is to throw errors only in fatal situations. > What's > your use case? If it's something common, we might consider an API change. > > Nick >
