Coder Social home page Coder Social logo

Comments (2)

guillermocalvo avatar guillermocalvo commented on May 27, 2024

exceptions4c provides a macro e4c_reusing_context which allows for exception handling inside library functions. This macro reuses an existing exception context, if available when the library function was called. If there is no current exception context, then begins a new one and automatically ends it when finished.

This is an example of the intended usage:

int library_public_function(void * pointer, int number){

    // We don't know where this function is going to be called from, so:
    //   - We cannot use "try", "throw", etc. right here, because the
    //     exception context COULD be uninitialized at this very moment.
    //   - We cannot call "e4c_context_begin" either, because the
    //     exception context COULD be already initialized.
    // If we have to make use of the exception handling system, we need
    // to "reuse" the existing exception context or "use" a new one.

    volatile int status = STATUS_OK;

    e4c_reusing_context(status, STATUS_ERROR){

        // Now we can safely use "try", "throw", etc.
        if(pointer == NULL){
            throw(NullPointerException);
        }

        library_private_function(pointer, number);
    }

    return(status);
}

I plan to enhance the documentation at the library home page but so far I haven't had the time to do it. In the meantime, you might want to take a look at the source code.

I hope this works for you; please let me know your thoughts about it.

from exceptions4c.

dereckson avatar dereckson commented on May 27, 2024

Yes, this works well, and indeed, the e4c.h documentation is well written and clear.

I would also suggest to include a sample with the signal handling (with the warning we catch host signals too, even if it's logic, it's well to warn about it), maybe using the normal e4c map instead of a custom one:

        const e4c_signal_mapping *old_mappings = e4c_context_get_signal_mappings();
        e4c_context_set_signal_mappings(e4c_default_signal_mappings);
        [...]
        e4c_context_set_signal_mappings(old_mappings);

Or are there signals we absolutely need to let the host intercept?

from exceptions4c.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.