How are exceptions implemented under the hood?

前端 未结 10 1443
粉色の甜心
粉色の甜心 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:44

    Regarding performance - sparse use of exceptions will probably have negligible effects, but do not abuse them.

    I have personally seen Java code which performed two orders of magnitude worse than it could have (took about x100 the time) because exceptions were used in an important loop instead of more standard if/returns.

    0 讨论(0)
  • 2020-12-07 08:45

    The best paper ever written on the implementation of exceptions (under the hood) is Exception Handling in CLU by Barbara Liskov and Alan Snyder. I have referred to it every time I've started a new compiler.

    For a somewhat higher-level view of an implementation in C using setjmp and longjmp, I recommend Dave Hanson's C Interfaces and Implementations (like Eli Bendersky).

    0 讨论(0)
  • 2020-12-07 08:46

    Here is a common way C++ exceptions are implemented:
    http://www.codesourcery.com/public/cxx-abi/abi-eh.html

    It is for the Itanium architecture, but the implementation described here is used in other architectures as well. Note that it is a long document, since C++ exceptions are complicated.

    Here is a good description on how LLVM implements exceptions:
    http://llvm.org/docs/ExceptionHandling.html

    Since LLVM is meant to be a common intermediate representation for many runtimes, the mechanisms described can be applied to many languages.

    0 讨论(0)
  • 2020-12-07 08:46

    C++ code at Google (save for some Windows-specific cases) don't use exceptions: cfr the guidelines, short form: "We do not use C++ exceptions". Quoting from the discussion (hit the arrow to expand on the URL):

    Our advice against using exceptions is not predicated on philosophical or moral grounds, but practical ones. Because we'd like to use our open-source projects at Google and it's difficult to do so if those projects use exceptions, we need to advise against exceptions in Google open-source projects as well. Things would probably be different if we had to do it all over again from scratch.

    This rule does not apply to Google code in other languages, such as Java and Python.

    0 讨论(0)
提交回复
热议问题