How can I throw an exception in C?

CExceptionSyntax

C Problem Overview


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

How can I do it in C?

C Solutions


Solution 1 - C

There are no exceptions in C. In C the errors are notified by the returned value of the function, the exit value of the process, signals to the process (Program Error Signals (GNU libc)) or the CPU hardware interruption (or other notification error form the CPU if there is)(How processor handles the case of division by zero).

Exceptions are 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.

Solution 2 - C

In C you could use the combination of the setjmp() and longjmp() functions, 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.

Solution 3 - C

There's no built-in exception mechanism in C; you need to simulate exceptions and their semantics. This is usually achieved by relying on setjmp and longjmp.

There are quite a few libraries around, and I'm implementing yet another one. It's called exceptions4c; it's portable and free. You may take a look at it, and compare it against other alternatives to see which fits you most.

Solution 4 - C

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.

Solution 5 - C

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:

gcc -o bar.o -c bar.c && g++ a.cc bar.o && ./a.out
Output
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.

Solution 6 - C

This question is super old, but I just stumbled across it and thought I'd share a technique: divide by zero, or dereference a null pointer.

The question is simply "how to throw", not how to catch, or even how to throw a specific type of exception. I had a situation ages ago where we needed to trigger an exception from C to be caught in C++. Specifically, we had occasional reports of "pure virtual function call" errors, and needed to convince the C runtime's _purecall function to throw something. So we added our own _purecall function that divided by zero, and boom, we got an exception that we could catch on C++, and even use some stack fun to see where things went wrong.

Solution 7 - C

On Windows with Microsoft Visual C++ (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.

Solution 8 - C

C doesn't have exceptions.

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

Solution 9 - C

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.

Solution 10 - C

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.

Solution 11 - C

If you write code with the happy path design pattern (for example, for an embedded device) you may simulate exception error processing (AKA deffering or finally emulation) with operator "goto".

int process(int port)
{
    int rc;
    int fd1;
    int fd2;

    fd1 = open("/dev/...", ...);
    if (fd1 == -1) {
      rc = -1;
      goto out;
    }

    fd2 = open("/dev/...", ...);
    if (fd2 == -1) {
      rc = -1;
      goto out;
    }

    // Do some with fd1 and fd2 for example write(f2, read(fd1))

    rc = 0;

   out:

    //if (rc != 0) {
        (void)close(fd1);
        (void)close(fd2);
    //}

    return rc;
}

It is not actually an exception handler, but it takes you a way to handle error at function exit.

P.S.: You should be careful to use goto only from the same or more deep scopes and never jump a variable declaration.

Solution 12 - C

Solution 13 - C

In C we can't use try case to handle the error. but if you can use Windows.h so you can:

#include <stdio.h>
#include <Windows.h>
#include <setjmp.h>
jmp_buf Buf;

NTAPI Error_Handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
	printf("co loi roi ban oi.!!!\r\n");
	longjmp(Buf, 1);
}

void main()
{
	AddVectoredExceptionHandler(1, Error_Handler);
	int x = 0;
	printf("start main\r\n");
	if (setjmp(Buf) == 0)
	{
		int y = 1 / x;
	}
	printf("end main\r\n");
	
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhttpinterpretView Question on Stackoverflow
Solution 1 - CBrian R. BondyView Answer on Stackoverflow
Solution 2 - CvavaView Answer on Stackoverflow
Solution 3 - CGuillermo CalvoView Answer on Stackoverflow
Solution 4 - CLucas JonesView Answer on Stackoverflow
Solution 5 - CwcyView Answer on Stackoverflow
Solution 6 - CJoeView Answer on Stackoverflow
Solution 7 - CDonal FellowsView Answer on Stackoverflow
Solution 8 - CJoeView Answer on Stackoverflow
Solution 9 - Cuser3510229View Answer on Stackoverflow
Solution 10 - CMahmoud Al-QudsiView Answer on Stackoverflow
Solution 11 - CVitold S.View Answer on Stackoverflow
Solution 12 - CalinsoarView Answer on Stackoverflow
Solution 13 - CNhân MaiView Answer on Stackoverflow