Question: I typed this into Google, but I only found how-tos in C++.

How can I do it in C?

Answer: 
In C you could use the combination of the `setjmp()` and `longjmp()` functions, defined in `setjmp.h`. [Example from Wikipedia](http://en.wikipedia.org/wiki/Setjmp.h)

```
#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.
C is able to throw C++ exceptions. It is machine code anyway.

For example, in file *bar.c*:

```
#include <stdlib.h>
#include <stdint.h>

extern void *__cxa_allocate_exception(size_t thrown_size);
extern void __cxa_throw (void *thrown_exception, void* *tinfo, void (*dest) (void *) );
extern void * _ZTIl; // typeinfo of long

int bar1()
{
   int64_t * p = (int64_t*)__cxa_allocate_exception(8);
   *p = 1976;
   __cxa_throw(p, &_ZTIl, 0);
  return 10;
}

```

In file *a.cc*,

```
#include <stdint.h>
#include <cstdio>
extern "C" int bar1();

void foo()
{
  try
  {
    bar1();
  }
  catch(int64_t x)
  {
    printf("good %ld", x);
  }
}

int main(int argc, char *argv[])
{
  foo();
  return 0;
}

```

To compile it:

```none
gcc -o bar.o -c bar.c && g++ a.cc bar.o && ./a.out

```

### Output

```none
good 1976

```

<https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html> has more detail info about `__cxa_throw`.

I am not sure whether it is portable or not, and I test it with 'gcc-4.8.2' on Linux.