Reputation: 845
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.
Upvotes: 5
Views: 6910
Reputation: 797
May I suggest you to take a look at my library exceptions4c? It's been tested on Linux and has many features, such as finally
blocks, signal handling and a kind of polymorphism that lets you create exception hierarchies. It also supports multi-threading.
Upvotes: 1
Reputation: 12951
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.
Upvotes: 1
Reputation: 3207
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
Upvotes: 1
Reputation: 10393
You can handle the signals by writing your signal handlers for it. Some of these signals documented at GNU are:
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.
Upvotes: 7
Reputation: 62031
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.
Upvotes: 3