Reader small image

You're reading from  C++ Programming for Linux Systems

Product typeBook
Published inSep 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805129004
Edition1st Edition
Languages
Right arrow
Authors (2):
Desislav Andreev
Desislav Andreev
author image
Desislav Andreev

Desislav Andreev is a software engineer with a PhD in artificial intelligence systems and quantum machine learning. He has several publications in software engineering and AI applications. For his 10 years in the field he has a demonstrated history of working in automotive software engineering and in the area of the higher education. He is skilled in system and software architectures, operating systems, C and C++ development, autonomous driving and computer graphics. He is currently working as a Lead C++ Developer in VMware, developing its core software functionalities. He is also a lecturer at the Technical University of Sofia. He was previously a Technical Professional and software architect in the CRE and ADAS departments of Visteon Corporation, working closely with both OEMs and development teams.
Read more about Desislav Andreev

Stanimir Lukanov
Stanimir Lukanov
author image
Stanimir Lukanov

Stanimir Lukanov is a C++ expert, software tech lead and architect at VMWare. He has more than 15 years of professional experience in writing efficient and robust C++ enterprise code. Stanimir is a member of the Bulgarian National Body which is part of The C++ Standards Committee (JTC1/SC22/WG21). His interests are in the area of software security for distributed enterprise software systems. Since 2017 he has worked at VMWare where he currently leads a team which develops core security functionality in one of the major products in the company's portfolio. Before joining VMWare he held the position of senior software engineer at Visteon Corporation and Johnson Controls. He was responsible for defining software architecture, making code reviews, leading C++ training and delivering fast and robust C++ code for real-time automotive embedded systems.
Read more about Stanimir Lukanov

View More author details
Right arrow

Handling Errors with C++

This chapter will focus on error handling in C++. As a programmer, you will inevitably encounter situations where you need to determine the best approach to propagate program errors. Whether you use error codes or exceptions, we will delve into them to gain a better understanding of how to use them effectively.

In this chapter, we will examine how to handle errors reported by POSIX APIs using C++. We will begin by covering the errno thread-local variable and the strerror function. After that, we will introduce std::error_code and std::error_condition and demonstrate how they help to wrap POSIX errors that come from POSIX APIs. We will also investigate custom error categories, which allow us to compare errors produced by various sources and develop platform-independent error-handling code.

As we progress, we will learn about exceptions in C++ and how to convert std::error_code into a std::system_error exception. We will also explore some best practices...

Technical requirements

All examples in this chapter have been tested in an environment with the following configuration:

Handling errors from POSIX APIs with C++

In POSIX-compliant systems, such as Unix and Linux, error handling is based on the use of error codes and error messages to communicate errors between functions and applications.

In general, when a function encounters an error, it returns a non-zero error code and sets the errno global variable to a specific error value that indicates the nature of the error. The application can then use the errno variable to determine the cause of the error and take appropriate action.

In addition to error codes, POSIX-compliant functions often provide error messages that describe the nature of the error in more detail. These error messages are typically accessed using the strerror function, which takes an error code as input and returns a pointer to a sequence of characters terminated with a null character containing the corresponding error message.

The POSIX error-handling style requires developers to check for errors after each system call or function...

From error codes to exceptions

Exception handling is an important aspect of programming, especially when dealing with errors that can disrupt the normal flow of a program. While there are several ways to handle errors in a code base, exceptions provide a powerful mechanism for handling errors in a way that separates error flow from normal program flow.

When working with error codes, it can be challenging to ensure that all error cases are properly handled and that the code remains maintainable. By wrapping error codes in exceptions, we can create a more pragmatic approach to error handling that makes it easier to reason about code and catch errors in a more centralized manner.

It’s hard to say which approach is better when dealing with error handling in a code base, and the decision to use exceptions should be based on pragmatic considerations. While exceptions can provide significant benefits in terms of code organization and maintainability, they may come with a performance...

Summary

This chapter has covered various techniques for error handling when working with POSIX APIs in C++. We discussed the use of errno, a thread-local variable, and the strerror function. We also explored how std::error_code and std::error_condition can wrap POSIX errors and how custom error categories enable us to compare errors generated by different sources and develop platform-independent error-handling code. Furthermore, we delved into exceptions in C++ and how to convert std::error_code into an exception of the std::system_error type.

We also examined best practices for working with exceptions, such as throwing them by value and catching them by reference, to avoid issues such as object slicing. Finally, we learned about the RAII technique in C++, which eliminates the need for a finally construct in the language.

In the next chapter, we will explore the topic of concurrency with C++.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Programming for Linux Systems
Published in: Sep 2023Publisher: PacktISBN-13: 9781805129004
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Desislav Andreev

Desislav Andreev is a software engineer with a PhD in artificial intelligence systems and quantum machine learning. He has several publications in software engineering and AI applications. For his 10 years in the field he has a demonstrated history of working in automotive software engineering and in the area of the higher education. He is skilled in system and software architectures, operating systems, C and C++ development, autonomous driving and computer graphics. He is currently working as a Lead C++ Developer in VMware, developing its core software functionalities. He is also a lecturer at the Technical University of Sofia. He was previously a Technical Professional and software architect in the CRE and ADAS departments of Visteon Corporation, working closely with both OEMs and development teams.
Read more about Desislav Andreev

author image
Stanimir Lukanov

Stanimir Lukanov is a C++ expert, software tech lead and architect at VMWare. He has more than 15 years of professional experience in writing efficient and robust C++ enterprise code. Stanimir is a member of the Bulgarian National Body which is part of The C++ Standards Committee (JTC1/SC22/WG21). His interests are in the area of software security for distributed enterprise software systems. Since 2017 he has worked at VMWare where he currently leads a team which develops core security functionality in one of the major products in the company's portfolio. Before joining VMWare he held the position of senior software engineer at Visteon Corporation and Johnson Controls. He was responsible for defining software architecture, making code reviews, leading C++ training and delivering fast and robust C++ code for real-time automotive embedded systems.
Read more about Stanimir Lukanov