Does Linux provide an exception handling in C without resorting to C++? Or, what might be the best way to implement such an exception handling? The goal is to avoid to check return codes for every function called, but do something like in C++ that is thread safe and easily portable.
You can handle the signals by writing your signal handlers for it. Some of these signals documented at GNU are:
- Program Error Signals: Used to report serious program errors.
- Termination Signals: Used to interrupt and/or terminate the program.
- Alarm Signals: Used to indicate expiration of timers.
- Asynchronous I/O Signals: Used to indicate input is available.
- Job Control Signals: Signals used to support job control.
- Operation Error Signals: Used to report operational system errors.
- Miscellaneous Signals: Miscellaneous Signals.
- Signal Messages: Printing a message describing a signal
You can get more info in depth about this here. It states the following which I suppose is what you are looking for:
If you anticipate an event that causes signals, you can define a handler function and tell the operating system to run it when that particular type of signal arrives.
I've never heard of Linux providing anything like that, but this page describes a third-party exception handling library for C: http://www.on-time.com/ddj0011.htm I haven't been able to find the download link, though.
The kernel does it by using goto to jump to the teardown sections.
See here for the coding standards: http://lxr.linux.no/linux+v2.6.34/Documentation/CodingStyle
Offtopic probably, but I can't resist it, sorry.
I must say, the only really good and comprehensive exception mechanism that I've seen so far is SEH - structured exception handling in Windows.
IT blows the C++ exception handling model (which raises the hands when exception is throw within the destructor of an automatic object during the stack unwinding).
Plus it's a really uniform exception handling, since it combines both software exceptions and those generated by the hardware.
So that if you want exception handling - either write for Windows, or implement something similar for Linux.
P.S. Unlike many people think, exception handling is far far more than just interrupting the normal program flow using jmp
.
It's also a chain of negotiations about who and how handles the exception. It's (most important) - the correct cleanup execution at each scope, dealing with nested exceptions and etc.