views:

195

answers:

6

I typed this into google but only found howtos in C++,

how to do it in C?

+8  A: 

There are no exceptions in C. Exceptions defined in C++ and other languages though.

Exception handling in C++ is specified in the C++ standard "S.15 Exception handling", there is no equivalent section in the C standard.

Brian R. Bondy
So in C it's guaranteed there won't be exceptions,how?
httpinterpret
@httpinterpret: If you have C code then there is no way to throw an exception like you're thinking. You're more likely to get a return code or fall into some code that has undefined behavior.
Brian R. Bondy
@httpinterpret: in C it's "guaranteed" that there are no exceptions in the same way that it's "guaranteed" that there are no templates, or no reflection, or no unicorns. The language specification simply doesn't define any such thing. There is setjmp/longjmp, though, which can be used to exit a function without returning. So programs could build their own exception-like mechanisms if they want, or a C implementation could define extensions to the standard.
Steve Jessop
A program with `main` in a `.c` file can include some C++, and therefore exceptions could be thrown and caught in the program, but the C code portions will remain ignorant of all of this going on except that exception throwing and catching often rely on functions written in C which reside in the C++ libraries. C is used because you can't risk the function called to do `throw` needing to throw an exception itself. There may be a compiler/library/target specific way to throw/catch exceptions, though. But throwing a class instance will have it's own problems.
nategoose
@Steve: Please let me know if you find a language with unicorns, I've been waiting for such a thing for years.
Brian R. Bondy
+2  A: 

C doesn't have exceptions.

There are various hacky implementations that try to do it (one example at: http://adomas.org/excc/).

Joe
+5  A: 

Plain old C doesn't actually support exceptions natively.

You can use alternative error handling strategies, such as:

  • returning an error code
  • returning FALSE and using a last_error variable or function.

See http://en.wikibooks.org/wiki/C_Programming/Error_handling.

Lucas Jones
+2  A: 

C doesn't support exceptions. You can try compiling your C code as C++ with Visual Studio or G++ and see if it'll compile as-is. Most C applications will compile as C++ without major changes, and you can then use the try... catch syntax.

Computer Guru
A: 

On Win with MSVC there's _try ... __except ... but it's really horrible and you don't want to use it if you can possibly avoid it. Better to say that there are no exceptions.

Donal Fellows
+1  A: 

In C you should use setjmp/longjmp defined in setjmp.h. Example from Wikipedia

#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

void second(void) {
    printf("second\n");         // prints
    longjmp(buf,1);             // jumps back to where setjmp 
                                //   was called - making setjmp now return 1
}

void first(void) {
    second();
    printf("first\n");          // does not print
}

int main() {   
    if ( ! setjmp(buf) ) {
        first();                // when executed, setjmp returns 0
    } else {                    // when longjmp jumps back, setjmp returns 1
        printf("main");         // prints
    }

    return 0;
}

Note: I would actually advise you not to use them as they work awful with C++ (destructors of local objects wouldn't get called) and it is really hard to understand what is going on. Return some kind of error instead.

vava
I've seen setjump/longjump not work correctly in C++ programs even when no objects that needed destruction when exceptions were used. I rarely use them in C programs.
nategoose