27

llvm/clang are considered good C++ code bases. I wonder why C++ exceptions arenot used in them at all?

Memory is managed using something like pools, and erros are reported with returnd values and codes like in C. They even wrapping operator new to be placement new that returns error and not exception when no memory.

Do you have idea why llvm philosophy is not to use C++ exceptions when most books recommend using them?

zaharpopov
  • 15,266
  • 20
  • 73
  • 89

6 Answers6

20

Chris Lattner recently clarified this issue in the LLVM project coding standards.

Not using exceptions and RTTI reduces executable size and reduces overhead. (You might argue that zero-cost exceptions have no overhead unless thrown. At a minimum, they actually break up the code into smaller basic blocks and inhibit some types of code motion.)

ohmantics
  • 1,790
  • 13
  • 16
6

Writing exception safe c++ code is a difficult task.

Turning off exceptions can speed up code execution and reduce code size.

Maybe this is related.

rotoglup
  • 5,034
  • 19
  • 37
  • 10
    Writing correct C++ code that isn't also exception-safe is far more difficult than just writing exception-safe code in the first place (at least basic exception safety). – James McNellis Nov 02 '10 at 21:45
  • 2
    I'm not able to pinpoint any specific link, but not using C++ exceptions is quite common. Google c++ style guide also mentions that exceptions are not used. Maybe I'm a little pessimistic but basic exception safety may soon become not enough when dealing with exceptions. Not as if I'm an advocate for or against exceptions though... – rotoglup Nov 02 '10 at 21:53
  • 13
    @rotoglup: it's true that the Google C++ style guide forbids exceptions, but it also explains that they didn't think this was a good decision any more, just something they were forced into by the need to freely interoperate with their own legacy code. – Steve Jessop Nov 02 '10 at 22:20
  • 4
    @Clayton: and humans are glorified apes ? There's just too much difference between the two of them to justify a proper reply... – Matthieu M. Nov 04 '10 at 15:39
  • 9
    @comonad: you're just plain wrong. `goto` allow arbitrary flow (going up, down...) while exceptions only allow going "down" ie to the next catch block that may handle them. And Stack Unwinding is guaranteed. Also, since we are talking C++, there is no finally block. Writing code with basic exception safety is easy, it is not taught in University and many have no idea of what it is, but ignorance and difficulty are two orthogonal concepts. I agree that writing strong exception safe code is more difficult, but it's not more difficult that providing the same guarantees with another mechanism... – Matthieu M. Nov 05 '10 at 07:28
  • don't know, why i did write that, yesterday. whatever... try/catch/throw is usually realized via , and so it is slow. – comonad Nov 06 '10 at 02:20
  • 5
    @ClaytonHughes Exception are not like gotos at all. Exceptions are like monads. It allows you to compose functions, and catch errors at a higher level. You can't do that with GOTO, especially in functional programming languages in which GOTO is impossible. `for`, `while`, `if`, `else` are glorified GOTOs, but exceptions are NOT like GOTOs at all. – Paul Fultz II Feb 08 '12 at 12:13
5

Depending on your compiler and your code, a program that uses exceptions can be faster or slower than an equivalent program that disables and doesn't use exceptions. Also the one that uses exceptions may be larger or smaller.

Every error handling strategy incurs some cost, and I expect that the LLVM developers considered their situation and found that disabling exceptions was the better decision for LLVM.

My recommendation, and the recommendation I have most seen from experts, is to use exceptions to report failures unless you have some specific, solid reason not to. If your reason is performance, it would be wise to base your choice on profiling. Remember that it is vital to compare code that uses exceptions to code that doesn't, but still handles errors correctly.

Todd Greer
  • 81
  • 1
  • 2
  • 5
    The books I've read on C++ style all advocate reporting errors with exceptions. This includes books written by Herb Sutter, Andrei Alexandrescu, and Bjarne Stroustrup. The standard library implicitly recommends exceptions, by virtue of using them to report errors. Quite simply, exceptions are the established standard means of reporting errors (though other means are perfectly appropriate to some contexts). – Todd Greer Oct 19 '12 at 20:05
2

I think this stems from another guideline: Use assert liberally

  • Normal error conditions are treated using error codes.
  • Exceptional error conditions are treated via assert.

I would say that an assert is an even harder exception: you definitely cannot ignore it ;)

Matthieu M.
  • 251,718
  • 39
  • 369
  • 642
  • @zaharpopov: yes, but it means that the software passed all the tests, and llvm/clang have an impressive test suite growing daily (and tested daily too :p) – Matthieu M. Nov 05 '10 at 07:23
0

Do most books recommend using them? I know most books on C++ programming cover them because they are part of the language, but I don't think I have seen a book that said to prefer them to error codes or other methods of error handling. In fact I would say most books implicitly do not recommend using exceptions because they don't cover how to write good exception safe code.

As far as LLVM being good code and it being not exception based code, the two concepts are largely orthogonal. Code can be cleanly written with or without exceptions.

stonemetal
  • 5,972
  • 21
  • 25
  • 4
    more effective c++ recommends prefer exceptions over c-like return codes because exceptions can't be ignored – zaharpopov Nov 02 '10 at 18:45
  • 3
    Read "The C++ Programming Language" – Steve M Nov 02 '10 at 19:16
  • 9
    It is true that most C++ books do not cover exception safety. It is also true that most C++ books aren't worth reading. The few that are worth reading _do_ cover exception safety, because it is of cricial importance when writing correct C++ code. – James McNellis Nov 02 '10 at 21:49
  • @ Steve M I have, I have. It doesn't change the fact that most programming books that teach a language cover syntax, not proper use or even safe use. Sure there is the occasional book that goes beyond K&R, The C++ Programming Language but look at how they are spoken of as if they were mystical. As the saying goes the exception proves the rule. – stonemetal Nov 03 '10 at 15:18
  • @smerlin: `assert` is not a replacement for exceptions, since `assert` can't be "handled" after it's fired. Exceptions are designed to be handled. – Mooing Duck Nov 28 '12 at 19:13
  • @MooingDuck: Yes you are correct, i made that comment a some time ago and it was wrong (you can not assert if a return value is used by checking if the copy constructor was called, since the copy constructor call can be optimized away). – smerlin Nov 29 '12 at 17:18
0

Seems it is not a llvm philosophy to avoid exceptions. At least I found nothing about exceptions in the coding standard (http://llvm.org/docs/CodingStandards.html), so it is up to developer.

Why it is not widely used? AFAIK exception support was implemented in llvm not so far, so it was not possible to compile llvm for llvm itself :). So it can be just a historical reason to avoid exceptions.

Yuras
  • 13,636
  • 1
  • 41
  • 58