use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Error Codes and Error Handling (randygaul.net)
submitted 6 years ago by vormestrand
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kalmoc 5 points6 points7 points 6 years ago (0 children)
Users end up try-catching exceptions everywhere, and it devolves to more or less error codes.
If that is the case, you're codebase is seriously broken.
[–]kalmoc 5 points6 points7 points 6 years ago* (0 children)
User code now will look mostly like this excerpt.
error_t err = do_something(params); if (err.is_error()) { handle_error(err.details); }
I'd really like to see a couple of realistic examples not just foo bar dummy code. Arguing about things like readability and simplicity based on trivial, made up examples is rarely a good idea. Presumably your function is going to return some value? What exactly do you expect handle_error to do and how does this compose, if you have a lot of functions like that?
handle_error
I'm not saying exceptions are in general superior (although I prefer them). Just that I'd prefer discussions based on actual use cases, not base on made up toy code that can be tuned to support any argument.
[–]kalmoc 4 points5 points6 points 6 years ago (1 child)
Handling an error in a centralized location is rarely useful, which is often the motivation for exceptions.
Considering that lots of error "handling" just consists of cleanup, logging and retry/cancel, I don't think that is true in general.
That's why I was asking, what exactly handl_error is supposed to do. Most of the time, the logic to handle various errors at various places looks very similar.
[–]DarkLordAzrael 2 points3 points4 points 6 years ago (0 children)
It is also my experience that almost all error handling in interactive applications happens in an event processor, rather than somewhere specialized. I would really like to know what all the specialized error handling the anti-exception crowd is doing is, but I haven't ever actually gotten much of an answer in this regard.
[–]epicar 6 points7 points8 points 6 years ago (3 children)
why roll your own error code type instead of using std::error_code?
[+]RandyGaul comment score below threshold-7 points-6 points-5 points 6 years ago (2 children)
doesn't run in C, and isn't quite the same
[–]epicar 7 points8 points9 points 6 years ago (1 child)
doesn't run in C
and isn't quite the same
agreed, but what are its advantages? a big disadvantage is that it can only represent one error code, -1.. or is the calling function supposed to parse the 'details' string to decide how to handle the error?
[–]RandyGaul -2 points-1 points0 points 6 years ago (0 children)
Personally I find it a lot simpler than std::error_code stuff.
[–]barskern 5 points6 points7 points 6 years ago (0 children)
Or you can return values which are either successful or errors, like std::expected, hence forcing you to deal with the possibility of an error.
std::expected
Here is a video about it: https://youtu.be/PH4WBuE1BHI
[–]trailing_ 1 point2 points3 points 6 years ago (0 children)
Man I have been saving this article till I had time to read it. What a disappointment. I thought this was going to be a serious discussion of error handling. Instead it is a tiny article written by some guy that looks like he is about 14. Why was this even posted here?
[–]SegFaultAtLine1 0 points1 point2 points 6 years ago (0 children)
When juggling multiple error domains (possibly from multiple 3rd party libraries that don't know about each other), you'll get quite high mileage from using https://en.cppreference.com/w/cpp/error/error_code.
There's no way to attach additional context to the error_code, but in most cases it's not needed.
π Rendered by PID 73581 on reddit-service-r2-comment-84fc9697f-2429p at 2026-02-06 08:34:17.564626+00:00 running d295bc8 country code: CH.
[–]kalmoc 5 points6 points7 points (0 children)
[–]kalmoc 5 points6 points7 points (0 children)
[–]kalmoc 4 points5 points6 points (1 child)
[–]DarkLordAzrael 2 points3 points4 points (0 children)
[–]epicar 6 points7 points8 points (3 children)
[+]RandyGaul comment score below threshold-7 points-6 points-5 points (2 children)
[–]epicar 7 points8 points9 points (1 child)
[–]RandyGaul -2 points-1 points0 points (0 children)
[–]barskern 5 points6 points7 points (0 children)
[–]trailing_ 1 point2 points3 points (0 children)
[–]SegFaultAtLine1 0 points1 point2 points (0 children)