views:

154

answers:

5

Possible Duplicate:
C : How do you simulate an 'exception' ?

Hi, I know that exception handling is available in C++ but not in C. But I also read that exception handling is provided from OS side, so isnt there any function for C invoking the same behavior from OS as try catch and throw? Thanks.

+6  A: 

The C language itself has no support for exception handling. However a means of exception handling for C does exist on a platform + compiler specific basis.

In windows for example C programs can use SEH exception handling.

I've seen arguments in the past that the C function pair setjmp and longjmp is exception handling in C. I consider it closer to the ejection pattern but it's worth investigating

JaredPar
+1  A: 

Not in a platform-independent way. And this would only be for hardware/OS-level exceptions, not SW exceptions.

Oli Charlesworth
+1  A: 

I don't think there is a true, pure, portable C solution. For Microsoft compilers, you can use __try __except, see http://msdn.microsoft.com/en-us/library/zazxh1a9(VS.80).aspx

Emile
+1  A: 

C provides exceptions in a standard library: setjmp()/longjmp().

Joshua
A: 

There's no support for exception handling. I would implement it through clever use of macros, signals and signal handlers (SIGUSR1/2). In practice, when you "throw", your app send a SIGUSR1 to its own pid. The signal handler you registered then gets the control, and you can put the exception details in a global variable. as for "catching" you can inspect the exception global variable and act according to its status.

it's far from easy, but you can try if you really want to feel wizardy.

EDIT : I though more about it, and what I am proposing is wrong. signal handling is performed asynchronously, so your exception won't be propagated correctly. Please downvote me (keeping the answer for future reference of a bad solution).

Stefano Borini