Among the many new library features in C++0x is a little header called <system_error>. It provides a selection of utilities for managing, well, system errors. The principal components defined in the header are:
- class error_category
- class error_code
- class error_condition
- class system_error
- enum class errc
I had a hand in the design of this facility, so in this series of posts I will try to capture the rationale, history, and intended uses of the various components.
Where to get it
A complete implementation, and one that supports C++03, is included in Boost. I'd guess that, at this point in time, it is probably the best tested implementation in terms of portability. Of course, you have to spell things starting with boost::system:: rather than std::.
An implementation is included with GCC 4.4 and later. However, you must compile your program with the -std=c++0x option in order to use it.
Finally, Microsoft Visual Studio 2010 will ship with an implementation of the classes. The main limitation is that the system_category() does not represent Win32 errors as was intended. More on what that means later.
Overview
Here are the types and classes defined by <system_error>, in a nutshell:
- class error_category - intended as a base class, an error_category is used to define sources of errors or categories of error codes and conditions.
- class error_code - represents a specific error value returned by an operation (such as a system call).
- class error_condition - something that you want to test for and, potentially, react to in your code.
- class system_error - an exception used to wrap error_codes when an error is to be reported via throw/catch.
- enum class errc - a set of generic error condition values, derived from POSIX.
- is_error_code_enum<>, is_error_condition_enum<>, make_error_code, make_error_condition - a mechanism for converting enum class error values into error_codes or error_conditions.
- generic_category() - returns a category object used to classify the errc-based error codes and conditions.
- system_category() - returns a category object used for error codes that originate from the operating system.
Principles
This section lists some of the guiding principles I had in mind in designing the facility. (I cannot speak for the others involved.) As with most software projects, some were goals at the outset and some were picked up along the way.
Not all errors are exceptional
Simply put, exceptions are not always the right way to handle errors. (In some circles this is a controversial statement, although I really don't understand why.)
In network programming, for example, there are commonly encountered errors such as:
- You were unable to connect to a remote IP address.
- Your connection dropped out.
- You tried to open an IPv6 socket but no IPv6 network interfaces are available.
Sure, these might be exceptional conditions, but equally they may be handled as part of normal control flow. If you reasonably expect it to happen, it's not exceptional. Respectively:
- The IP address is one of a list of addresses corresponding to a host name. You want to try connecting to the next address in the list.
- The network is unreliable. You want to try to reestablish the connection and only give up after N failures.
- Your program can drop back to using an IPv4 socket.
Another requirement, in the case of Asio, was a way to pass the result of an asynchronous operation to its completion handler. In this case, I want the operation's error code to be an argument to the handler callback. (An alternative approach is to provide a means to rethrow an exception inside the handler, such as .NET's BeginXYZ/EndXYZ asynchronous pattern. In my opinion, that design adds complexity and makes the API more error-prone.)
Last, but not least, some domains will be unable or unwilling to use exceptions due to code size and performance constraints.
In short: be pragmatic, not dogmatic. Use whatever error mechanism suits best in terms of clarity, correctness, constraints, and, yes, even personal taste. Often the right place to make the decision between exception and error code is at the point of use. That means a system error facility should support both.
Errors come from multiple sources
The C++03 standard recognises errno as a source of error codes. This is used by the stdio functions, some math functions, and so forth.
On POSIX platforms, many system operations do use errno to propagate errors. POSIX defines additional errno error codes to cover these cases.
Windows, on the other hand, does not make use of errno beyond the standard C library. Windows API calls typically report their errors via GetLastError.
When one considers network programming, the getaddrinfo family of functions uses its own set of error codes (EAI_...) on POSIX, but shares the GetLastError() "namespace" on Windows. Programs that integrate other libraries (for SSL, regular expressions, or whatever) will encounter other categories of error code.
Programs should be able to manage these error codes in a consistent manner. I'm especially concerned with enabling composition of operations to create higher-level abstractions. Combining system calls, getaddrinfo, SSL and regular expressions into one API should not force the user of that API to deal with an explosion of error code types. The addition of a new error source to the implementation of that API should not change the interface.
Be user-extensible
Users of the standard library need to be able to add their own error sources. This ability may just be used to integrate a third-party library, but is also tied in with the desire to create higher-level abstractions. When developing a protocol implementation such as HTTP, I want to be able to add a set of error codes corresponding to the errors defined in the RFC.
Preserve the original error code
This was not one of my original goals: my thinking was that the standard would provide a set of well-known error codes. If a system operation returned an error, it was the responsibility of the library to translate the error into a well-known code (if such a mapping made sense).
Fortunately, someone showed me the error of my ways. Translating an error code discards information: the error returned by the underlying system call is lost. This may not be a big deal in terms of program control flow, but it matters a lot for program supportability. There is no doubt that programmers will use a standardised error code object for logging and tracing, and the original error may be vital in diagnosing problems.
This final principle segues nicely into my topic for part 2: error_code vs error_condition. Stay tuned.
247 comments:
«Oldest ‹Older 201 – 247 of 247I had a great day reading this blog! home painters
This is great! Glad to check this out. pool enclosure
Having faith increases your sense of serenity and trust.
Iman Gadzhi Net Worth
Great insight into handling system errors and asynchronous thinking in C++! Robust error handling is crucial for modern applications. Looking forward to more in this series. Thanks for sharing!
Such a nice article i ever seen, good work man workplace emergency response procedures perth / dispute resolution lawyers Perth
System error support in C++0x - part 1 is the best for the users who need to know here. There are a lot of people who need to get help to get them off your roof to resolve the issues that provide us with the right results.
This is great! Glad to check this out. roof installation
Useful content. Our team highly appreciate your post.
Nice intro! I’ve heard of but never really looked into it—curious to see what kind of utilities it offers for handling system-level issues. Looking forward to the wholesale beach supplies breakdown of those components!
Thank you so much for sharing this informative blog with us. <a href="https://grapevine-masonry.com/>brick mailbox</a>
This is a tough pill to swallow, but it’s necessary to face the reality of this situation. iPhone 17 Pro Max Waterproof Case
if any user want to add FREE Backlink of their website or submit seo link building free with high TF/CF site you can check this site - https://backlinkyourwebsite.com
Thanks for sharing this excellent deep dive into the C++0x header—it's a crucial part of modern C++ error handling that often gets overlooked. Your perspective as someone involved in its design adds a lot of valuable context, especially around the motivations for including both error codes and error conditions, and the deliberate separation of categories. marsp1anet.com/
Excited to follow along — it’s always fascinating to hear the behind-the-scenes insights from someone directly involved in the design. Looking forward to learning about the thought process and history behind the facility . www.seo-tampa.org/
I'm amazed by how interesting and informative this blog is! brick mason
Thank you so much for taking the time on sharing this with us. retaining wall blocks
I'm excited to share a behind-the-scenes look at this facility—I was involved in its design, so in this series of posts, I’ll walk through the thinking, history, and purpose behind the different parts of it. Naples Chiropractor
Hi! I thought I had been to this site before, but after looking through some of the posts, I realized it was new to me. Anyway, I'm glad I found it and I'll definitely be bookmarking it and checking back often storage units port Charlotte fl
This article provides a good overview of the header in C++0x, highlighting its key components and design principles. It's helpful to understand how to manage system errors effectively without relying solely on exceptions.
Learning stuff brought by this website is so cool!
Regards,
drywall installer
I really appreaite everyone's help
75 Hard Challenge Diet</a
Awesome post: Crispy Chicken Sandwiches
Thanks for sharing this detailed and insightful post on system_error in C++0x! This is a thoughtful explanation, clearly written by someone deeply involved in the design and practical use of the facility. https://miamishoresplumbing.com/
Thank you very much for this wonderful topic: 15 Best Things to Do in Mauritius
Good to know that Boost provides a full implementation, especially with support for older standards like C++03. That kind of backward compatibility can be really helpful in legacy projects lansing web development .
Elevate your streetwear game with the New York Yankees Nike black jacket – a must-have for fans who crave both comfort and bold style. Designed for die-hard Yankees supporters, this sleek jacket blends team pride with Nike’s premium athletic craftsmanship. new york nike black jacket Whether you're heading to Yankee Stadium or styling up for a night out, this black bomber delivers sharp looks and all-season versatility. Perfect for collectors and trendsetters alike, it also makes a standout choice for those searching for celebrity-inspired jackets, MLB outerwear, or the latest Nike sports jackets.
Thank you for sharing this post: Raize EV Unveiled
Keep on sharing such an interesting blog! concrete slab
This is an excellent and informative write-up on the motivation and design of the library introduced in C++0x (now known as C++11). Since you had a hand in its design, your insights provide valuable context for those trying to understand how to use it and, just as importantly, why it was built the way it was. ppcpros.co/
That's awesome work! Having a well-designed home page and a detailed tutorial will definitely help web design company lansing new users get up to speed with Criterion. The added inline documentation and examples are a big bonus too—it's great to see the project becoming more accessible and developer-friendly.
This post does a great job introducing the motivations and design principles behind the header in C++0x (later C++11), offering valuable historical context for why it was developed and how it fits into real-world programming needs. basement remodels
I appreciate you providing this useful information: Hero Splendor 2025
Thank you so much for taking the time on sharing this with us. demolition
Great Post: Yamaha Aerox 155
This writeup is a clear and informative introduction to the facility in C++0x (now known as C++11), and it’s especially valuable since it's written from the perspective of someone involved in the design. chainlink fence
I love it: Yamaha MT 15 V2
Awesome post: Suzuki Burgman 400 Launched
That's great to know—Boost really is a lifesaver when it comes to compatibility with older standards like C++03. Having a full implementation included saves a ton of time! Fort Mitchell Roofing Company
I love to visit such an interesting and informative blog like this! lawn care
A system error can occur due to software bugs, hardware failures, or configuration issues, disrupting normal operations. It's essential to identify the root cause quickly to prevent further complications. For instance, unexpected errors can impact websites like roswelltreeservices.com, affecting user access or service functionality. Regular maintenance and updates help minimize such disruptions and ensure systems run smoothly and efficiently.
The Provided information on you blog is too good. Önemli
Great article, it’s very educational and unique. Please keep us updated on any updates.
önemli
The idea of System error support in C++0x - part 1 is good, and many developers are taking interest in it. At Buderim Roof Restoration I saw amazing solutions, and it is good to have such places where we can learn how to deal with these issues.
I'm grateful for your wise counsel.
Hyundai Exter Facelift 2025
I'm grateful for your wise counsel.
Hyundai Exter Facelift 2025
freddie mercury yellow jacket is rugged, authentic, and full of frontier spirit! 🤠🔥 Whether it's Rip Wheeler’s iconic jacket, cowboy hats, or Dutton-branded tees and caps, each piece channels that Western grit and family loyalty. Perfect for fans who want to wear their ranch pride loud and proud—just like living on the Yellowstone. 🐎 #YellowstoneGear #DuttonStyle
Post a Comment