How can I assert() without using abort()?

后端 未结 6 1041
失恋的感觉
失恋的感觉 2020-12-08 20:06

If I use assert() and the assertion fails then assert() will call abort(), ending the running program abruptly. I can\'t afford that

6条回答
  •  暖寄归人
    2020-12-08 20:39

    Asserts in C/C++ only run in debug builds. So this won't happen at runtime. In general asserts should mark things that if they happen indicate a bug, and generally show assumptions in your code etc.

    If you want to have code that checks for errors at runtime (in release) you should probably use exceptions rather than asserts as these are what they are designed to do. Your answer basically wraps an exception thrower in assert syntax. While this will work, there is no particular advantage to this that I can see over just throwing the exception in the first place.

提交回复
热议问题