Thread: Recommend a good exception library?

  1. #1
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72

    Recommend a good exception library?

    Can anyone recommend a good exception library? It needs to be for C not C++. (hence the reason I posted in this forum)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ITAmember View Post
    Can anyone recommend a good exception library? It needs to be for C not C++. (hence the reason I posted in this forum)
    I'm not aware of any good exception libraries. I've seen numerous implementations, some of them quite good, but not in general purpose library form.

    All of the reasonable implementations use a stack of cleanup handlers. They vary in the method used to return control up the execution stack during an exception. Some implementations use explicit returns, and some use longjmp.

    The longjmp approach is inherently wrong, and can never be done safely, because of the local-variable-clobber issue. However, in simple cases it is tolerable. Explicit returns can be made somewhat more aesthetic by appropriate wrapper macros. Neither solution even approaches the usability of C++ exceptions.

    So no, I can't recommend a specific library, but I can discuss techniques if you want to implement one yourself.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    How difficult would it be to implement one? Or, more importantly, how long would it take?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ITAmember View Post
    How difficult would it be to implement one? Or, more importantly, how long would it take?
    Depending on your skill level and what kind of functionality you need, probably a day to a week.

    You could evaluate the library bithub recommended. But the author himself calls the thing a "hack."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by ITAmember View Post
    How difficult would it be to implement one? Or, more importantly, how long would it take?
    It's pretty non-trivial; especially if you start considering thread safety issues. The most common approach to implement exception style code in C is just to use gotos (This is what the Linux kernel does).

    Code:
    int foo(void)
    {
        int ret_val = ERROR;
    
        /* ... */
        if(bad_case)
            goto error;
    
        ret_val = SUCCESS;
    error:
        /* function cleanup goes here */
        return ret_val;
    }

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bithub View Post
    It's pretty non-trivial; especially if you start considering thread safety issues.
    In what sense? Each thread needs its own cleanup stack, but other than that, I don't think there are any thread safety issues that you wouldn't also encounter in C++. Generally, exception handlers should not touch data which is shared between threads.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Each thread needs its own cleanup stack
    That's pretty much what I meant. In a single threaded app, there can just be a single cleanup stack. In a multi-threaded app, you need to store one per thread, and then have synchronization around whatever container holds all the cleanup stacks. It's not that difficult to implement, but it does add another layer of complexity.

  9. #9
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    I downloaded Exceptions in C to try it out. The problem is it won't compile. I uploaded the three files as attachments. The test0.c file came with Exceptions in C. I made a project with all three files and tried to compile. I got this error message:

    `try' undeclared (first use in this function)

    any ideas?

  10. #10
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    The two C files included exception-local.h not exception.h! No wonder it didn't work, it was including the wrong file! Anyway changed it and it works just fine now, it looks like Exceptions in C is just what I need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  4. good vector library
    By rotis23 in forum C Programming
    Replies: 4
    Last Post: 10-08-2002, 11:49 AM
  5. Anyone recommend a good editor
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-30-2001, 12:54 AM