Problems with C++ exceptions

(marler8997.github.io)

70 points | by signa11 a day ago ago

103 comments

  • SuperV1234 a day ago ago

    This post completely misunderstands how to use exceptions and provides "solutions" that are error-prone to a problem that doesn't exist.

    And this is coming from someone that dislikes exceptions.

    • mixedmath 3 hours ago ago

      I'm not very familiar with proper exception usage in C++. Would you mind expanding a bit on this comment and describing the misunderstanding?

    • marler8997 a day ago ago

      I avoid using exceptions myself so I wouldn't be surprised if I misunderstand them :) I love to learn and welcome new knowledge and/or correction of misunderstandings if you have them.

      I'll add that inspiration for the article came about because It was striking to me how Bjarne's example which was suppose to show a better way to manage resources introduced so many issues. The blog post goes over those issues and talks about possible solutions, all of which aren't great. I think however these problems with exceptions don't manifest into bigger issues because programmers just kinda learn to avoid exceptions. So, the post was trying to go into why we avoid them.

      • xerokimo 17 hours ago ago

        RAISI is always wrong because the whole advantage of the try block is to write a unit of code as if it can't fail so that what said unit is intended to do if no errors occur is very local.

        If you really want to handle an error coming from a single operation, you can create a new function, or immediately invoke a lambda. This would remove the need break RAII and making your class more brittle to use.

        You can be exhaustive with try/catch if you're willing to lose some information, whether that's catching a base exception, or use the catch all block.

        If you know what all the base classes your program throws, you can centralize your catch all and recover some information using a Lippincott function.

        I've done my own exploring in the past with the thought experiment of, what would a codebase which only uses exceptions for error handling look like, and can you reason with it? And I concluded you can, there's just a different mentality of how you look at your code.

      • thegrim33 a day ago ago

        No offense, but why did you decide to write an instructional article about a topic that you "wouldn't be surprised that you misunderstand"? Why are you trying to teach to others what you admittedly don't have a very solid handle on?

        • marler8997 a day ago ago

          None taken :) I think sharing our thoughts and discussing them is how we learn and grow. The best people in their craft are those who aren't afraid to put themselves out there, even if they're wrong, how else would you find out?

    • guy2345 a day ago ago

      what about the misreported errno problem?

      • binary132 a day ago ago

        obviously the errno should have been obtained at the time of failure and included in the exception, maybe using a simple subclass of std exception. trying to compute information about the failure at handling time is just stupid.

  • smallstepforman a day ago ago

    My biggest beef with exceptions is invisible code flow. Add the attribute throws<t> to function signature (so that its visible) and enforce handling by generating compiler error if ignored. Bubbling up errors is OK. In essence, this is result<t,e>. Thats OK. Even for constructors.

    What I dislike is having a mechanism to skip 10 layers of bubbling deep inside call stack by a “mega” throw of type <n> which none of the layers know about. Other than

    • gpderetta a day ago ago

      I would also like to have type checked exceptions in c++ (as long as we can also template over exception specifications).

      But what I would really want is noexcept regions:

         <potentially throwing code>...
         noexcept {
            <only noexcept code here> ...
         }
         <potentially more throwing code>...
      
      i.e. a way to mark regions of code that cannot deal with unwind and must only call non-throwing operations.
    • wakawaka28 12 hours ago ago

      Don't think of the uncaught type as a "mega" throw. It's just a distinct type of error that nobody specified that they can handle. If you truly worry about the caller missing something, then somewhere in there you can catch anything and translate into a recognizable exception. This is easiest to understand in a library. The interface functions can catch all and translate into one particular exception type for "unknown" or generic errors. Then, that will be caught by anyone using the thing as documented. This only works if it's just reporting a non-fatal error. In case of a fatal error, it can't be handled, so the translation is kind of pointless.

  • Flundstrom2 a day ago ago

    Java and Rust are the only language that I know of that has proper handling of exceptions; mandatory declaration as part of method declaration, since exceptions ARE an integral part of the contract. (Yes, I consider the Result<T, E> being corresponding to exception declaration, since the return value MUST be checked prior to use of T.)

    • nielsbot a day ago ago

      Swift user here: I have to say one of the best features of Swift is the exception handling. Which is to say, exceptions in Swift are not C++/Java/Obj-C style exceptions, but instead are a way to return an error result from a function. And Swift enforces that the error is handled.

      That is, a `throw` statement in Swift simply returns an `Error` value to the caller via a special return path instead of the normal result.

      More explicitly, a Swift function declared as:

          func f() throws -> T {
          }
      
      Could be read as

          func f() -> (T|any Error) {
          }
      
      
      More here: https://github.com/swiftlang/swift/blob/main/docs/ErrorHandl...
      • thomasmg a day ago ago

        I saw that in Swift, a method can declare it throws an exception, but it doesn't (can't) declare the exception _type_. I'm not a regular user of Swift (I usually use Java - I'm not sure what other languages you are familiar with), but just thinking about it: isn't it strange that you don't know the exception type? Isn't this kind of like an untyped language, where you have to read the documentation on what a method can return? Isn't this a source of errors itself, in practise?

        • Someone a day ago ago

          > isn't it strange that you don't know the exception type?

          Java experience taught us that, when writing an interface, it is common not to know the exception type. You often can’t know, for example, whether an implementation can time out (e.g. because it will make network calls) or will access a database (and thus can throw RollbackException). Consequently, when implementing an interface, it is common in Java to wrap exceptions in an exception of the type declared in the interface (https://wiki.c2.com/?ExceptionTunneling)

          • thomasmg a day ago ago

            Yes I know Java and the challenges with exceptions there (checked vs unchecked exceptions, errors). But at least (arguably) in Java, the methods (for checked exceptions at least) declares what class the exception / exceptions is. I personally do not think wrapping exceptions in other exception types, in Java, is a major problem. In Swift, you just have "throws" without _any_ type. And so the caller has to be prepared for everything: a later version of the library might suddenly return a new type of exception.

            One could argue Rust is slightly better than Java, because in Rust there are no unchecked exceptions. However, in Rust there is panic, which is in a way like unchecked exceptions, which you can also catch (with panic unwinding). But at least in Rust, regular exceptions are fast.

            • Someone a day ago ago

              > And so the caller has to be prepared for everything: a later version of the library might suddenly return a new type of exception.

              But you get the same with checked exceptions in Java. Yes, an interface will say foo can only throw FooException, but if you want to do anything when you get a FooException, you have to look inside to figure out what exactly was wrong, and what’s inside that FooException isn’t limited.

              A later version of the library may suddenly throw a FooException with a BarException inside it.

          • magicalhippo a day ago ago

            What I liked about Bosst's error_code[1], which is part of the standard library now, is that it carties not just the error but the error category, and with it a machinery for categories to compare error_codes from other categories.

            So as a user you could check for a generic file_not_found error, and if the underlying library uses http it could just pass on the 404 error_code with an http_category say, and your comparison would return true.

            This allows you to handle very specific errors yet also allow users to handle errors in a more generic fashion in most cases.

            [1]: https://www.boost.org/doc/libs/latest/libs/system/doc/html/s...

          • tcfhgj a day ago ago

            When using a language forcing checked exceptions, you would know, wouldn't you?

        • mayoff a day ago ago

          Swift gained limited support for “typed throws” in Swift 6.0 (2024).

          https://github.com/swiftlang/swift-evolution/blob/main/propo...

          I say limited because the compiler doesn't (yet, as of 6.2) perform typed throw inference for closures (a closure that throws is inferred to throw `any Error`). I have personally found this sufficiently limiting that I've given up using typed throws in the few places I want to, for now.

        • amomchilov 12 hours ago ago

          Typed exceptions are unlike typed parameters or return values. They don’t just describe the interface of your function, but expose details about its implementation and constrain future changes.

          That’s a huge limitation when writing libraries. If you have an old function that declares that it can throw a DatabaseError, you can’t e.g. add caching to it. Adding CacheError to the list of throwable types is an API breaking change, just like changing a return type.

          Swift has typed errors now, but they shouldn’t be used carefully, and probably not be the default to reach for

        • nielsbot 16 hours ago ago

          I don't think it's strange at all--my main uses of the returned errors are

          1a. yes, there was some error 1b. there was an error--throw another local error and encapsulate the caught error 2. treat result of throwing call as `nil` and handle appropriately

          I don't think typed throws add anything to the language. I think they will result in people wasting time pondering error types and building large error handling machines :)

          When I used Java, I found typed exceptions difficult to reason about and handle correctly.

        • a day ago ago
          [deleted]
      • mayoff a day ago ago

        Another really nice thing about Swift is that you have to put the `try` keyword in front of any expression that can throw. This means there's no hidden control flow: if some function call can throw, you're informed at the call site and don't have to look at the function declaration.

      • fakwandi_priv a day ago ago

        From what I can read Swift gives you a stack trace which is good. At the moment I’m using Go where that stack is only generated where the panic is triggered, which could be much higher up. Makes it a lot more unwieldy to figure out where an error happens because everyone uses:

        > if err != nil return err

        • nielsbot 16 hours ago ago

          This is built in to the language.

          When you call code that can throw (return an error via the special return path) you either have to handle it or make the enclosing context also throwing.

          Assuming `canThrow()`, a function that might throw an `Error` type:

              func canThrow() throws {
                  ...
              }
          
          
          Call canThrow(), don't handle errors, just rethrow them

              func mightThrow() throws {
                  try canThrow() // errors thrown from here will be thrown out of `mightThrow()`
                  ...
              }
          
          Alternatively, catch the errors and handle them as you wish:

              func mightThrow() throws {
                  do {
                      try canThrow()
                  } catch {
                      ...handle error here
                      ...or `throw` another Error type of your choosing 
                  }
                  ...
              }
          
          There are a few more ways to handle throwing calls.. For example

          - `try?` (ignore error result, pretend result was `nil`)

          - `try!` (fail fatally on error result)

        • mayoff a day ago ago

          Swift doesn't capture a stack trace in the `Error` object, but Xcode can break when an error is thrown if you set a “Swift Error Breakpoint”, and the debugger will show you the stack trace. Under the hood it just sets breakpoints on the runtime functions `swift_willThrow` and `swift_willThrowTypedImpl`.

      • binary132 a day ago ago

        that sounds very similar to noexcept(bool) to me, except that noexcept can be computed, for example by deriving from some generic trait, and we presume throws unless specified non-throwing by noexcept.

    • morshu9001 a day ago ago

      In high-level code, pretty much everything can fail in many different ways, and usually you're either just passing the error up or handling it in some catchall manner. Rust's behavior makes sense for its use cases, but it'd get exhausting doing this in like a web backend.

      • wahern a day ago ago

        It's already too exhausting to use for OOM, which is why Rust effectively punted on that from the very beginning. And the ironic thing is that anyhow::Error (or similar) seems poised to become idiomatic in Rust, which AFAIU always allocates, while the extremely belated try_ APIs... does anybody even use them?

        It's a shame. Were I designing a "low-level" or "systems" language, rather than put the cart before the horse and pick error results or exceptions, my litmus test would be how to make handling OOM as easy as possible. Any language construct that can make gracefully handling OOM convenient is almost by definition the fabled One True Way. And if you don't have a solution for OOM from the very beginning (whether designing a language or a project), it's just never gonna happen in a satisfactory way.

        Lua does it--exceptions at the language level, and either setjmp/longjmp or C++ exceptions in the VM implementation. But for a strongly typed, statically compiled language, I'm not sure which strategy (or hybrid strategy) would be best.

        • MindSpunk a day ago ago

          I think Rust's choice to panic on OOM is the right choice for 95% of code people write. It would be absurdly verbose, and OOM can be very tricky to recover from without careful top down design because it's very easy for error handling code to itself try and allocate (print/format a message) leading to a process abort. The mistake was not having the fallible APIs on allocating containers from the start, for the users that do care and can tangibly recover.

          Depending on the OS you likely won't even get the chance to handle the error because the allocation never fails, instead you just over commit and kill the process when physical memory gets exhausted.

          I guess what I'm trying to say is designing your language's error handling primitives around OOM is probably not a good idea, even in a systems programming language, because it's a very rare class of generally unrecoverable error that only very carefully designed code can recover from.

          Anyhow allocating isn't that absurd either. It keeps the size of Result<T, E> down by limiting the 'E' to be a pointer. Smaller Result<T, E> can help the compiler generate better code for passing the return value, and the expectation is the error path is rare so the cost of allocating might be outweighed by the lighter Result<T, E> benefits. Exceptions take a similar standpoint, throwing is very slow but the non-exceptional path can (theoretically) run without any performance cost.

          • binary132 a day ago ago

            you mean kind of like dynamically implementing std::exception? hmm.

        • morshu9001 a day ago ago

          Hm, not sure how you'd do this. OOM error auto bubbling up sounds dangerous because the inner code might not leave things in a consistent state if it exits unexpectedly, so that makes sense to manually handle, but it's tedious. Rust at least has nice ? and ! syntax for errors, unlike Go where the error-prone error handling actually ruins the entire language.

          • binary132 a day ago ago

            If only there were some sort of automated idiom for cleaning up state under failure modes

        • aw1621107 a day ago ago

          > while the extremely belated try_ APIs... does anybody even use them?

          I want to say the main driver for those right now is Rust for Linux since the "normal" panicking behavior is generally undesirable there.

    • munchler a day ago ago

      `Result<T, E>` comes from Haskell's `Either a b` type. F# also has a `Result<'T, 'E>` type.

      It's funny how often functional programming languages lead the way, but imperative languages end up with the credit.

      • raincole a day ago ago

        Actually (insert nerd emoji) this is a direct descendant from tagged union type, which existed in ALGLO 68, an imperative language.

        Java's checked exception is just an (very anti-ergonomic) implementation of tagged union type.

        • munchler a day ago ago

          OK, but it wasn't until functional programming came along that tagged unions were recognized as a natural way to implement sum types, which is how they are used in modern programming. The old idea of a "variant" has pretty much faded away.

      • paulddraper a day ago ago

        Algebraic types, immutable structures, lambdas, higher-order functions.

        I think FP receives a lot of "credit."

        Albeit the "pure" FP languages aren't popular, because 99% FP is really hard.

    • barrkel a day ago ago

      Don't forget, failure modes pierce abstraction boundaries. An abstraction that fully specifies failure modes leaks its implementation.

      This is why I think checked exceptions are a dreadful idea; that, and the misguided idea that you should catch exceptions.

      Only code close to the exception, where it can see through the abstraction, and code far away from the exception, like a dispatch loop or request handler, where the failure mode is largely irrelevant beyond 4xx vs 5xx, should catch exceptions.

      Annotating all the exceptions on the call graph in between is not only pointless, it breaks encapsulation.

      • binary132 a day ago ago

        This is a better way of expressing what I had been thinking about putting exception details behind an interface, except that in my mind encapsulating errors is just good design rather than implementation hiding, since the programmer might want to express a public error API, for example to tell the user whether a given fopen failed due to not finding the file or due to a filesystem fault.

      • 1718627440 a day ago ago

        If your error codes leak the implementation details through the whole call stack you are doing it wrong. Each error code describes what fails in terms of it's function call semantics. A layer isn't supposed to just return this upwards, that wouldn't make sense, but to use it to choose it's own error return code, which is in the abstraction domain of it's function interface.

        • barrkel 6 hours ago ago

          So you want to gradually reduce the fidelity of the error message as it makes its way up the stack.

          That means that the top level handler can at best log a vague message.

          That in turn means you must log along the way where you have more precise information about the failure, or you risk not having enough information to fix issues.

          And that in turn means you must have lots of redundant logging, since each point in the stack doesn't know whether the abstraction it's invoking has already logged or not, or encapsulation would be violated.

          • 1718627440 4 hours ago ago

            > That in turn means you must log along the way where you have more precise information about the failure

            Yes, that's the idea. You split the information into a diagnostic with stuff you deal with now and data that the upper layer will handle. The intersection between the data in these two things should be empty.

            > And that in turn means you must have lots of redundant logging, since each point in the stack doesn't know whether the abstraction it's invoking has already logged or not, or encapsulation would be violated.

            No. You print a diagnostic, about what exactly THIS layer was trying to do, you don't speculate what the upper layer was trying to do and try to log that. Every layer knows the lower layer has already logged the primary error, because an error object exists, and it also knows that the upper layer will print a diagnostic about what was the intention, so it only prints exactly what the error was in this layer.

            > doesn't know whether the abstraction it's invoking has already logged or not, or encapsulation would be violated.

            It knows that the lower layer has logged all stuff that that layer considered to be important, and that none of the data that is available to this layer was logged, since that is the responsibility of the caller.

            An example:

                Document rendering incomplete, skipped publishing step.  Thumbnail #39 missing.  Failed to fetch image: Connection refused. [Discarded malformed packet with SYN flag.  Invalid data in src/network/tcp.c:894 parse_tcp_packet_quirks_mode]
            
            Depending on the log level, you wouldn't show the later diagnostics. If there is a debug flag set, you can also add the function/line information to every step, not just to the last. If you are outtputing to stuff like syslog, you would put each diagnostic on its own line.
            • barrkel 2 hours ago ago

              The problem with this is you don't have context on subgraphs of control flow which have already returned by the time of the error.

              I think our disagreement is less about error handling and more about logging policy. I favour a logging policy which lets you understand what the code is doing even if it doesn't error out; this means that you don't need to log on unwind. You favour a logging policy specific for errors.

              My position is that your position ends up with less useful context for diagnosing errors.

              • 1718627440 an hour ago ago

                There is no reason you can use that scheme only for errors? In fact I don't.

                I use "unwinding" for diagnostics even in the happy case.

    • JoshTriplett a day ago ago

      In fairness, Rust also has unchecked exceptions (panics, if you use panic-unwind).

      • majormajor a day ago ago

        Java too has unchecked exceptions, though the post praising "mandatory declaration" didn't mention it for either.

        But they CAN be caught with a standard catch which may be non-intuitive if you don't know about them, and about that, in advance.

    • paulddraper a day ago ago

      It causes big problems in Java.

      For example, it plays poorly with generics. (Especially if you start doing FP, lambdas.)

      If Java added union types, it wouldn't be a big deal, but AFAIK this is still a limitation.

  • mgaunard a day ago ago

    Author completely misunderstands how to use exceptions and is just bashing them. A lot of what he says is inaccurate if not outwardly incorrect.

    Also Bjarne's control of C++ is quite limited, and he is semi-retired, so asking him to "fix his language" is fairly misguided. It's designed by a committee of 200+ people.

    Anyway what you want seems to be to not use exceptions, but monads instead. These are also part of the standard, it's called std::expected.

    • gblargg a day ago ago

      Agreed. Author is trying to mix paradigms. Simplest approach if they want local handling and non-propagation of errors is to just have the file holder not check for open success, and check that manually after construction. Then you get guaranteed closure of file no matter how the function is exited.

        class File_handle {
            FILE *p;
        public:
            File_handle(const char *pp, const char *r)  { p = fopen(pp, r); }
            ~File_handle() { if ( p ) fclose(p); }
            FILE* file() const { return p; }
        };
        
        
        void f(string s)
        {
            File_handle fh { s, "r"};
            if ( fh.file() == NULL ) {
                fprintf(stderr, "failed to open file '%s', error=%d\n", p, errno);
                return;
            }
            // use fh
        }
      • actionfromafar a day ago ago

        Goes against "Resource acquisition is initialization" though to have objects which exist but can't be used. (I think that's the relevant pattern?)

        However, where the language and it's objects and memory ends and the external world begins, like files and sockets... that's always tricky.

      • mikhael a day ago ago

        this may lose the value of errno, right?

        • dardeaup a day ago ago

          Yes, it would. It would be trivial to add errno as an instance variable for File_Handle class though.

    • matu3ba a day ago ago

      > Author completely misunderstands how to use exceptions and is just bashing them. A lot of what he says is inaccurate if not outwardly incorrect.

      Do you mind to elaborate what you believe are the misunderstandings? Examples of incorrect/inaccurate statements and/or an article with better explanations of mentioned use cases would be helpful.

      > it's called std::expected

      How does std::expected play together with all other possible error handling schemas? Can I get unique ids for errors to record (error) traces along functions? What is the ABI of std::expected? Stable(ish) or is something planned, ideally to get something C compatible?

      • mgaunard a day ago ago

        Well, he's using try/catch locally. You're not supposed to handle errors locally with exceptions. The whole point is inversion of control, you manage them at the point where you can do meaningful recovery, which then triggers the cleanup of the whole stack of frames once the exception bubbles up, rather than systematically cleaning up on the normal control flow path.

        Regardless, in his example, he could achieve what he wants by wrapping the try in a lambda, and returning either the value from try or nullopt from catch. But clearly, that's just converting exceptions to another error-handling mechanism because he isn't doing it right.

        He claimed that not handling an exception causes the program to crash, that's just plain incorrect. To be fair many people use the term "crash" liberally.

        std::expected or equivalent is often used with std::error_code, which is an extensible system (error codes are arranged in categories) that among others interops with errno.

        • 1718627440 a day ago ago

          > Well, he's using try/catch locally. You're not supposed to handle errors locally with exceptions.

          Tell that to Bjarne!

      • imron a day ago ago

        > Do you mind to elaborate what you believe are the misunderstandings?

        Exceptional C++, by Herb Sutter is an excellent resource that explains this. It’s quite outdated these days but the core concepts still hold well.

        When done well, most of your code can be happy path programming with errors/invalid state taken care of mostly automatically.

        However it’s also very easy not to do this well, and that ends up looking like the suggestions the author of the article makes.

    • ignition a day ago ago

      https://godbolt.org/z/363oqqKfv

        struct File_handle {
            static auto Create(std::string const & pp, const char *r) -> std::expected<File_handle, std::error_code> {
                auto p = fopen(pp.c_str(), r);
                if (!p) return std::unexpected(error_code_from_errno(errno));
                return File_handle{p};
            }
            ~File_handle() { fclose(p); }
        private:
            File_handle(FILE * f) : p{f} {} 
            FILE *p;
        };
    • a day ago ago
      [deleted]
    • lynx97 a day ago ago

      Most criticism of C++ comes from people not really into the language. Sure, learning curve might be an issue. But if you are really into C++, there are a lot of things to like about it. At least I do love it. However, only since C++11. Before that, the language felt very strange to me, possibly due to the same effect, I didn't know enough about it.

      • ranger_danger a day ago ago

        I'm the opposite. I like C++ but only until C++11. From that point onward, the rules got WAY more complicated, and there's only so much I can/want to hold in my brain at once, I just prefer simpler rules I guess.

        Occasionally I do like to use auto or lambdas from C++11 but even then I have to remember more rules for initialization because braces were introduced.

        • CoastalCoder a day ago ago

          This is true of me as well. I started with C++ in 1991, and I generally knew the language spec quite well (as far as users go) up through C++11.

          But now the language packs so much complexity that I can't be sure I understand all of the code I'm looking at.

          My usual quality standard for production code is that it doesn't just need to be correct, it needs to be obviously correct. So that's a problem.

          Doubly so when the other programmers on the team aren't C++ geeks.

  • barrkel a day ago ago

    You probably do want that exception to bubble up, actually. You probably don't want to catch it immediately after open. Because you need to communicate a failure mode to your caller, and what are you going to do then? Throw another exception? Fall back to error codes? Unwind manually with error codes all the way up? And if so, logging was the wrong thing to do, since the caller is probably going to log as well, based on the same philosophy, and you're going to get loads of error messages for one failure mode, and no stack trace (yes, there are ways of getting semi-decent stack traces from C++ exceptions).

    Exception safety has a lot of problems in C++, but it's mostly around allowing various implicit operations to throw (copies, assignments, destructors on temporaries and so forth). And that does come down to poor design of C++.

    • 1718627440 a day ago ago

      > you're going to get loads of error messages for one failure mode

      > and no stack trace

      That loads of error messages, meaning every layer describes what it tried to do and what failed, IS a user readable variant of a stack trace. The user would be confused with a real stack trace, but nice error messages serve both the user and the developer.

      • barrkel 6 hours ago ago

        This is error-prone boilerplate that obscures the code, obscures the logs and is a known antipattern (log and throw) - which you're implementing manually, by hand, in the hope you never make a mistake.

        You shouldn't do manually what you can automate.

        Boilerplate can make you feel productive and can give you warm fuzzies inside when you see lots of patterns that look familiar, but seeing the same patterns over and over again is actually a smell; it's the smell of a missing abstraction.

        • 1718627440 4 hours ago ago

          No. If you want to have the same information without that approach, you need to implement nested levels of error information, ten layers deep, each layer having a diagnostic, a log level, and file, line information, and a reason. In other words, you are building your own custom stack trace object, with annotated diagnostics. In addition, you can't reason about this at the upper level anyway, or you are rebuilding your application stack at some other place. The only thing you can do is to unwrap that object and serialize it into a diagnostic, which you could have done with less code, less memory and less compute. In addition, you would need to allocate on a failure path, which sounds like a nightmare.

          Also you can never have comments in the code, because what you would write into the comment is now in the diagnostic itself. That means you can also turn on DEBUG_LEVEL_XXL and get a nice description, what the program actually does and why.

          > This is error-prone

          Why is it error-prune. You receive an error of one type and need to convert it into an error of another type. You need to handle that or the compiler will bark.

          > obscures the logs

          How does it obscure the logs?

          • barrkel 2 hours ago ago

            The information about what the code was doing at the time an exception is thrown is implicit in the stack trace; the control flow is evident from line numbers, and with half-decent tooling, you can get hyperlinks from stack traces to source code.

            If there's extra information you need to know about what's going on, you probably want to log it in non-error cases too, because it'll contextualize an error before the error occurs. You can't log on unwind for decisions made in routines that were invoked and returned from, and are no longer on the stack, in the error case.

            > Why is it error-prune. You receive an error of one type and need to convert it into an error of another type. You need to handle that or the compiler will bark.

            This is a choice you've made, not something inherent to unchecked exceptions. My argument is that you should not generally change the type of the error, and let polymorphism cover the abstraction. It's error prone because it's code you need to write. All code you write is on the cost side of the ledger and liable to contain mistakes.

            > How does it obscure the logs?

            You get lots of error log messages for a single error.

            > In addition, you would need to allocate on a failure path, which sounds like a nightmare.

            I wanted to address this separately. Allocating on the failure path is only a real problem in OOM or severe memory corruption scenarios, in which case logging may not be available and the best course of action is probably to abort the program. In this case, you want the logs that led up to the abort, rather than trying to log positively under severe memory conditions.

            Are you a Go user by any chance? I've stayed away from Go precisely because of its boilerplate-reliant error handling mechanism. Or if you're stuck with C++ (with or without exceptions), my commiserations.

            • 1718627440 10 minutes ago ago

              > The information about what the code was doing at the time an exception is thrown is implicit in the stack trace; the control flow is evident from line numbers, and with half-decent tooling, you can get hyperlinks from stack traces to source code.

              Yes, but when you want to generate a diagnostic from that stack trace, you basically need to branch on all the possible internal states of internal layers at the point you catch the exception. So either you are incapable of generating detailed diagnostics or you essentially model the whole behaviour in a single place. Also the point where the error messages are generated is now completely removed from the place where the error did occur. This sounds like a nightmare to maintain and also means that the possible error messages aren't there as documentation when reading the code.

              > If there's extra information you need to know about what's going on, you probably want to log it in non-error cases too, because it'll contextualize an error before the error occurs. You can't log on unwind for decisions made in routines that were invoked and returned from, and are no longer on the stack, in the error case.

              I never said, that you can only use this for error cases. In fact what is an error and what not, is not defined in a single layer. For example a failure to open a file will be an error in a lower layer, but for an upper layer, that just means that it should try a different backend. Or a parsing error is fatal for the parser, but it might mean that the file format is different and the next parser should be tried, or that the data is from the network and can simply be discarded. An empty field can be normal data for the parser, but for the upper layer it is a fatal error.

              > This is a choice you've made, not something inherent to unchecked exceptions. My argument is that you should not generally change the type of the error, and let polymorphism cover the abstraction.

              Then either the reported errors are completely unspecific and unactionable or you are leaking implementation details. When you want to handle every error specifically and not leak implementation details, you need to handle it locally. When you want to know that you handle all cases, unchecked exceptions are unsatisfying. In my opinion programs should know what is going on and not just say "my bad, something happened I can't continue". That does not lead to robust systems and is neither suitable for user transparency nor for automated systems.

              In my opinion software should either work completely automated or report to the end user. Software that needs sysadmins and operators at runtime is bad. That doesn't mean that that never occurs, it will, but it should be treated as a software defect.

              > You get lots of error log messages for a single error.

              Yes, but this describes the issue at different layers of the abstraction and in my eyes the whole thing is a single error message. Neither the fact that resource X isn't available nor the fact that some connection was refused, is a complete error description in isolation. You need both for a (complete) error description.

              > Allocating on the failure path is only a real problem in OOM

              Yes, but first I don't like my program to act bad in that case, and second, it is also a nightmare for predictable ownership semantics. I generally let the caller allocate the error/status information.

              > Are you a Go user by any chance?

              I have never used Go, not even tried, but what I read about the error mechanism appealed to me, because it matches what I think is a good idea and do anyway.

              > Or if you're stuck with C++ (with or without exceptions), my commiserations.

              I don't feel that unhappy with that approach. I think this is a design and architectural decision rather than a language issue.

              > with or without exceptions

              Currently, definitely without, because it isn't even available when targeting a free-standing implementation (embedded), but I also don't prefer them, it makes for unpredictable control flow and makes it hard to reason about sound-, complete- and exhaustiveness.

              You seem to have the impression, that you can just panic and throw a stacktrace. That might work fine for a program running in the terminal and targeting developers, but it is not acceptable for end users nor for libraries. I also know programs that just output a stacktrace and crash. That is stupid. I mean I understand what is going on, because I am a developer, but first I am not familiar with every codebase I use, and second the average end user is not able to act on any of that and will be angry for good reason when it's documents are gone, data is corrupted or even the workflow is interrupted. I also don't perceive a network error, a file (system) issue or OOM to that rare for it to be acceptable, to just ignore it. I should be part of normal program behaviour.

  • lealanko a day ago ago

    This is a common problem with try-catch syntax. An alternative, and arguably more useful syntax would be

      try (File_handle fh {s, "r"}) {
        // use fh
      } unless (const File_error& e) {
        // handle error
      }
    
    Where the "use fh" part is not covered by the exception handler. This is covered (in ML context) in https://www.microsoft.com/en-us/research/publication/excepti...
    • mgaunard a day ago ago

      Don't use try/catch in the first place; that's where his error lies.

      • 1718627440 a day ago ago

        That's not his own invention, but an idea introduced by the language founder to sell the idea of RAII and exceptions. He did say, that he would prefer the C version, but then why use C++ in the first place.

  • febusravenga a day ago ago

    I think i agree with commenters that article kind-of uses exceptions wrong.

    But this shows problems with C++ exceptions, C++ codebases are literred with bad exception usage because "normal" programmers don't get it. Most of didn't get it for long time. So, they are complex enough that their usage is risk.

    Anyway, IMO C++ exceptions have two fundametal problems:

    * lack of stacktrace, so actually lack of _debugabbility_, that's why people try/catch everything

    * destructor problem (that actually there are exceptions tht cannot be propagated further and are lost

  • binary132 a day ago ago

    This strikes me as all wrong. The whole point of exceptions is control flow and destructors. By getting rid of RAII for the sake of simplifying the callsite a little, the author fails to obtain the real advantage, which is automatic resource unwinding of all local resources under failure conditions both during initialization and usage.

    If you want to simplify the callsite, just move the exception handling to a higher scope. I can admit it’s a little irritating to put a try/catch in main but it’s trivial to automate, and most programs are not written inline in main.

    The main problems I see with destructors have to do with hidden control flow and hidden type information. That said, hiding exceptional control flow from the mainline control flow of a function is also a useful feature, and the “exception type” of any given function is the recursive sum of all the exception types of any functions or operators it calls, including for example allocation. That quickly becomes either an extremely large and awkward flat sum or an extremely large and awkward nested sum, with awkward cross-namespace inclusion, and it becomes part of the hard contract of your API. This means, transitively, that if any deep inner call needs to extend or break its error types, it must either propagate all the way up into all of your callers, or you must recover and merge that error into _your_ API.

    For _most_ usecases, it is just simpler to implement a lean common interface such as std::exception and allow callers who care to look for more detailed information that you document. That said, there is a proposal (P3166 by Lewis Baker) for allowing functions to specify their exception set, including via deduction (auto):

    https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p31...

  • pmontra a day ago ago

    Or do without exceptions for control flow. For example Elixir does have try/catch but it's used very rarely because most functions return tuples with the first element being either :ok or :error. Then we can pattern match it and return an :error to the caller if we have to, possibly bubbling up several levels return after return. Or let the process crash and restart while the rest of the application keeps running. A surprising number of errors eventually fix themselves (API calls, disk space, missing data) when you design the system to attempt more times to complete its tasks. That's not only a characteristic of Elixir and the BEAM languages. You can do it more or less easily on any language. Maybe you need a queue and workers reading from the queue and all it takes to manage them, and BEAM makes it convenient by including most of it.

    The page about try/catch explains it well https://hexdocs.pm/elixir/try-catch-and-rescue.html

  • wvenable a day ago ago

    I agree with the other comments that this understanding of exceptions is wrong. He's not wrong about these two points:

    - Correctness: you don’t know if the exception type you’ve caught matches what the code throws

    - Exhaustiveness: you don’t know if you’ve caught all exceptions the code can throw

    But that's actually not a problem. Most of the time you shouldn't catch a specific exception (so always correct) and if you are catching then you should catch them all (exhaustive). A better solution is merely:

        void f(string s)
        {
            try {
                File_handle fh { s, "r"};
                // use fh
            } catch (const std::exception& e) {
                printf(stderr, e.what());
            }
        }
    
    That's all you need. But actually this code is also bad because this function f() shouldn't have a try/catch in it that prints an error message. That's a job for a function (possibly main()) further up the call stack.
  • gorgolo a day ago ago

    I didn’t really understand the writer’s comments with exceptions and I don’t code in C++.

    Their main complaint about exceptions seems to be that you can’t handle all of them and that you don’t know which you’ll get? If we compare this to python, what’s the difference here? It looks like it works the same here as in python; you catch and handle some exceptions, and others that you miss will crash your program (unless you catch the base class). Is there something special about C++ that makes it work differently, or would the author have similar problems with python?

    • marler8997 a day ago ago

      "You can't handle all of them and you don't know which you'll get" is a great summary of the first two problems, and, this same problem also applies to Python. I'll add that these only start becoming an issue when you start adding more exceptions to your codebase, especially if those exceptions start appearing deep in a callstack and seemingly unrelated code starts needing to be aware of them/handle them.

      The third problem (RAISI) is a C++ specific problem that Python doesn't have. Partly because in Python try/catch doesn't introduce a new scope and also partly because Python tends not to need a lot of RAII because of the nature of interpreted languages.

      I found this video a fascinating take on comparing C++ to Python if you haven't seen it: https://www.youtube.com/watch?v=9ZxtaccqyWA

    • apple1417 a day ago ago

      In normal use it's essentially the same yes. The one interesting edge case that might catch some people out is there's actually nothing special about std::exception, you can throw anything, "throw 123;" is valid and would skip any std::exception handlers - but you can also just catch anything with a "catch (...)".

    • 1718627440 a day ago ago

      > would the author have similar problems with python?

      I would expect yes. It is true, that in a lot of modern languages you need to live with that dynamism. But to people used to C, not knowing that the error handling is exhaustive, feels deeply uncomfortable.

  • charcircuit a day ago ago

    >The first is that our error message may not be correct. It’s possible that the exception we’ve caught was not introduced by opening this file, and, the errno may not reflect the errno at the time fopen was called.

    All that is needed is a better File_error type that includes the error that happened.

      void f(string s)
      {
          try {
              File_handle fh { s, "r"};
              // use fh
          } catch (const File_error& e) {
              fprintf(stderr, "File error: %s\n", e.msg.c_str();
              return;
          }
      }
  • a day ago ago
    [deleted]
  • jari_mustonen a day ago ago

    The classic "Frequently Questioned Answers" is the ultimate takedown of this abomination of a language.

    https://yosefk.com/c++fqa/

  • miohtama a day ago ago

    It's a bad post, but I find it funny someone thinks C++ can be fixed (:

  • senderista a day ago ago

    Or, if you don't want to go to the trouble of writing an RAII wrapper class for FILE*, just use scope_guard and (after determining that fopen() succeeded) register a lambda to close the FILE* on exiting the function (including by throwing an exception). I'm not a huge fan of scope_guard (or defer() in other languages), but it gets the job done for one-off cases.

  • howtofly a day ago ago

    All this hassle can be avoided by using `cleanup` compiler attribute.

    Manage classical C resources by auto-cleanup variables and do error-handling the normal way. If everything is OK, pass the ownership of these resources from auto-cleanup variables to C++ ctor.

    Note this approach plays nicely with C++ exception, and will enter C standard in the form of `defer`.

    • ranger_danger a day ago ago

      Yes, but now your code is no longer C or C++ standards compliant as it relies on compiler-specific attributes, if that matters to the programmer.

      Unfortunately, even the Linux kernel is no longer C because they use GCC compiler extensions (and are currently discussing adding MS ones too).

      • 1718627440 a day ago ago

        A kernel will make use of asm, and can't abstract over the machine, so it will always be unportable and relying on compiler extensions.

        • ranger_danger a day ago ago

          The Linux kernel did not always rely on compiler extensions though...

          • 1718627440 a day ago ago

            The kernel once did not need asm? That's a compiler extension, although a popular one.

  • oakpond a day ago ago

    The first two problems can be solved in a straightforward way with more custom exception types. For the "bigger problem", catch(...) can be used to prevent your code from crashing. If you really want to handle each case explicitly you could also use enums in combination with compiler flags that enable exhaustive checking.

  • _dky a day ago ago

    If C++ had a contract on what exceptions a function can throw with compile time check to enforce caller catches those exceptions, would it make it better?

    Guess Java does that, not much experience in Java here.

  • leduyquang753 a day ago ago

    > … go on to talk about removing the required scope to avoid RAISI .. then go on to talk about compile-time enforcement of exception handling…

    Is this an LLM prompt?

  • muragekibicho a day ago ago

    Honestly, I thought the diatribe would focus on needless complexity.

    The starting example is how I'd do it in C:

    ```

    void f(const char* p) // unsafe, naive use

    {

        FILE \*f = fopen(p, "r");    // acquire
    
        // use f
    
        fclose(f);                  // release
    
    }

    ```

    Wouldn't the simpler solution be ensuring your function doesn't exit before release? All that c++ destroyer stuff appears somewhat unnecessary and as the author points out, creates even more problems.

    • nanolith a day ago ago

      In C, you're correct. The problem is that, in C++, one must account for the fact that anything could throw an exception. If something throws an exception between the time that f is opened and f is closed, the file handle is leaked. This is the "unsafe" that Bjarne is talking about here. Specifically, exception unsafety that can leak resources.

      As an aside, it is one of the reasons why I finally decided to let go of C++ after 20 years of use. It was just too difficult to teach developers all of the corner cases. Instead, I retooled my system programming around C with model checking to enforce resource management and function contracts. The code can be read just like this example and I can have guaranteed resource management that is enforced at build time by checking function contracts.

      • dmpk2k a day ago ago

        Could you elaborate on the model checking? You have two codebases then (model and C), or something more integrated?

        • nanolith a day ago ago

          The function contracts are integrated into the codebase. Bounded model checking tools, such as CBMC, can be used to check for integer UB, memory safety, and to evaluate custom user assertions. The latter feature opens the door for creating function contracts.

          I include function contracts as part of function declarations in headers. These take the form of macros that clearly define the function contract. The implementation of the function evaluates the preconditions at the start of the function, and is written with a single exit so the postconditions can be evaluated at the end of the function. Since this function contract is defined in the header, shadow functions can be written that simulate all possibilities of the function contract. The two are kept in sync because they both depend on the same header. This way, model checks can be written to focus on individual functions with any dependencies simulated by shadows.

          The model checks are included in the same project, but are separate from the code under instrumentation, similar to how unit tests are commonly written. I include the shadow functions as an installation target for the library when it is installed in development mode, so that downstream projects can use existing shadow functions instead of writing their own.

    • mayoff a day ago ago

      The problem is that it's easy to do it wrong and the C compiler doesn't help you. RAII prevents you from leaking the resource, but the complaint in the post is that it can be cumbersome to use RAII in C++ if acquisition can fail and you want to handle that failure.

    • mgaunard a day ago ago

      That means you cannot use early exit, and all your variables must be checked as to whether they were initialized (which on top of the checks might also require further state).

      • themafia a day ago ago

        You can use goto to jump to one of several exit conditions based on the level of cleanup you need. It also nicely unifies all error exits into one place. The kernel makes heavy use of this style.

        • mgaunard a day ago ago

          sure, but that has limitations as well, since gotos can't cross lexical scopes, so you can't introduce variables later on, and it's easy to mess up.

          Destructors are a higher-level and safer approach.

          • 1718627440 a day ago ago

            > since gotos can't cross lexical scopes, so you can't introduce variables later on

            That's a C++ specific limitation, it works just fine in C.

  • classified a day ago ago

    If you use names that frigging bad, exceptions are the least of your problems.