How are exceptions implemented under the hood?

前端 未结 10 1467
粉色の甜心
粉色の甜心 2020-12-07 07:43

Just about everyone uses them, but many, including me simply take it for granted that they just work.

I am looking for high-quality material. Languages I use are: Ja

10条回答
  •  孤城傲影
    2020-12-07 08:28

    In his book C Interfaces and Implementations: Techniques for Creating Reusable Software, D. R. Hanson provides a nice implementation of exceptions in pure C using a set of macros and setjmp/longjmp. He provides TRY/RAISE/EXCEPT/FINALLY macros that can emulate pretty much everything C++ exceptions do and more.

    The code can be perused here (look at except.h/except.c).

    P.S. re your question about Google. Their employees are actually allowed to use exceptions in new code, and the official reason for the ban in old code is because it was already written that way and it doesn't make sense to mix styles.

    Personally, I also think that C++ without exceptions isn't the best idea.

提交回复
热议问题