Exception Handling in C without C++ in Linux

我的未来我决定 提交于 2020-02-01 04:18:47

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/3581818/exception-handling-in-c-without-c-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!