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.
The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.
As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.
try-catch – We use the try-catch block for exception handling in our code. try is the start of the block and catch is at the end of the try block to handle the exceptions. We can have multiple catch blocks with a try block. The try-catch block can be nested too.
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
Not in a platform-independent way. And this would only be for hardware/OS-level exceptions, not SW exceptions.
C provides exceptions in a standard library: setjmp()/longjmp().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With