XCode C language Exception

Discussion in 'Mac Programming' started by higher, Dec 28, 2008.

  1. higher macrumors newbie

    Joined:
    Dec 1, 2008
    #1
    how can i implement a funciton to catch exception in c laguage?
    like windows _try{} _ecxeption{}
    can anybody help me? Thanks in advance.
    Below is the ways that i used but not useful:
    1: signal() this can be ok in leopard system, but will be caught exception in tiger(system 10.4.11)
    2: InstrallExceptionHandler() all thread using this function must be run before this function was called, otherwise this will be not used for other thread.
    3: MPSetExceptionHandler() some time is ok, but some time doesn't run well.


    Is there anyother funciton to catch exception ?
     
  2. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    Exceptions aren't supported in the C language. Relying on a Microsoft embrace&extend of the language is not a great idea, IMO.

    If you want good exceptions, use C++ and get proper support for it. Barring that, you will need to use something like signal() while making sure that you setup a handler function for SIGUSR1 and/or SIGUSR2. Otherwise, the system will likely catch it for you and terminate the process.

    You don't want to send anything other than SIGUSR1 or SIGUSR2 when raising a signal, as everything else has a specific purpose that you may not be able to override.

    Another thing you could do is write your own _try{} _catch{} macros.

    It would probably look something like:

    Code:
    #define _try     int __exceptionVariable = 0
    
    #define _throw(_x) __exceptionVariable = _x; goto __catchLabel
    
    #define _catch  __catchLabel : if(__exceptionVariable != 0)
    
    The 'catch' with this code is that you would only be able to have one try/catch block per function, and wouldn't really correctly handle exceptions thrown from inside other methods. If you sat and thought about it for awhile, it may be possible to make more complex macros, but there would always be limitations since true exception support really needs to be built into the language (so it can clean up stacks and the like without you having to do it yourself).
     
  3. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #3
    For grins, I threw together another set of macros which can handle more complex exception handling. This version can throw an exception from one location on the stack, and return to another location on the stack. It can also more reliably handle being used multiple times in the same block of code. Not too resilient towards being nested though. It may work as-is, it may not. It should at least be close enough that someone can wrap it up and create their own homebrew exception handler for C.

    This code will need to be in a header:

    Code:
    extern jmp_buf __exceptionEnv;
    
    #define _try                                                    \
        int __exceptionInteger = 0;                                 \
        jmp_buf __previousState;                                    \
        memcpy(__previousState, __exceptionEnv, sizeof(jmp_buf));   \
        if(__exceptionInteger = setjmp(__exceptionEnv) == 0)        \
    
    #define _throw(_exceptionInt)                                   \
        longjmp(__exceptionEnv,_exceptionInt);
        
    #define _exceptionValue()    _exceptionInt
    
    #define _catch                                                  \
        memcpy(__exceptionEnv, __previousState, sizeof(jmp_buf));   \
        if(__exceptionInteger != 0)
    
    This code will need to be in a *.c file:

    Code:
    jmp_buf __exceptionEnv;
    
    And this is the use of it:

    Code:
    
    _try 
    {
        /* Do some stuff */
        if(!succeeded) {
            _throw(someIntValue);
        }
    }
    _catch
    {
        int myException = _exceptionValue();
        /* Handle exception here */
    }
    
    
     
  4. higher thread starter macrumors newbie

    Joined:
    Dec 1, 2008
    #4
    Thanks Krevnik

    it's useful but it's not suited for me that the exception is so many that i don't want to set a judgement.
    signal() i used well in leopard but crash in tiger.
     
  5. Macgenie macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #5
    Objective C

    You will find that X Code 3 / Obj c 2.0 now supports the "try" syntax - type @try in your class source code, X Code will fill out the bare bones of a "try" exception.
     
  6. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #6
    I'm not sure I understand. You are looking for a try/catch behavior, and the macros are intended to replicate that. You don't have to use an int type to pass the exception around either, you can use more complex types if you need.

    Which works in Obj-C, but not C, which I think is the limitation here.
     

Share This Page