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

How can I do it in C?

Answer: 
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.
As mentioned in numerous threads, the "standard" way of doing this is using setjmp/longjmp. I posted yet another such solution to <https://github.com/psevon/exceptions-and-raii-in-c>
This is to my knowledge the only solution that relies on automatic cleanup of allocated resources. It implements unique and shared smartpointers, and allows intermediate functions to let exceptions pass through without catching and still have their locally allocated resources cleaned up properly.