What are some “good” ways to use longjmp/setjmp for C error handling?

前端 未结 6 1061
南旧
南旧 2020-12-24 14:09

I have to use C for one project and I am thinking of using longjmp/setjmp for error handling as I think it will be much easier to handle error in one central pl

6条回答
  •  抹茶落季
    2020-12-24 14:10

    I have only ever found one use for setjmp()/longjmp() and it wasn't to do with error handling.

    There really is no need to use it for that since it can always be refactored into something easier to follow. The use of setjmp()/longjmp() is very similar to goto in that it can be easily abused. Anything that makes your code less readable is a bad idea in general. Note that I'm not saying they're inherently bad, just that they can lead to bad code easier than the alternatives.

    FWIW, the one place they were invaluable was a project I did in the early days of the industry (MS-DOS 6 time frame). I managed to put together a co-operative multi-threading library using Turbo C which used those functions in a yield() function to switch tasks.

    I'm pretty certain I haven't touched them (or had the need to) since those days.

提交回复
热议问题