Book Image

Advanced C++ Programming Cookbook

By : Dr. Rian Quinn
Book Image

Advanced C++ Programming Cookbook

By: Dr. Rian Quinn

Overview of this book

If you think you've mastered C++ and know everything it takes to write robust applications, you'll be in for a surprise. With this book, you'll gain comprehensive insights into C++, covering exclusive tips and interesting techniques to enhance your app development process. You'll kick off with the basic principles of library design and development, which will help you understand how to write reusable and maintainable code. You'll then discover the importance of exception safety, and how you can avoid unexpected errors or bugs in your code. The book will take you through the modern elements of C++, such as move semantics, type deductions, and coroutines. As you advance, you'll delve into template programming - the standard tool for most library developers looking to achieve high code reusability. You'll explore the STL and learn how to avoid common pitfalls while implementing templates. Later, you'll learn about the problems of multithreaded programming such as data races, deadlocks, and thread starvation. You'll also learn high-performance programming by using benchmarking tools and libraries. Finally, you'll discover advanced techniques for debugging and testing to ensure code reliability. By the end of this book, you'll have become an expert at C++ programming and will have gained the skills to solve complex development problems with ease.
Table of Contents (15 chapters)

Using Exceptions for Error Handling

In this chapter, we will learn some advanced C++ exception handling techniques. We assume here that you have a basic understanding of how to throw as well as catch a C++ exception. Instead of focusing on the basics of C++ exceptions, this chapter will teach you some of the more advanced techniques of C++ exception handling. This includes the proper use of the noexcept specifier and the noexcept operator so that you can properly mark your APIs as either possibly throwing an exception or explicitly not throwing a C++ exception, instead of calling std::terminate() when an error occurs that cannot be handled.

This chapter will also explain what the term Resource Acquisition is Initialization (RAII) is and how it complements C++ exception handling. We will also discuss why you should never throw a C++ exception from a class's destructor and how to handle these types of issues. Finally, we will look at how to create your own custom C++ exceptions including providing some basic guidelines on what to do and what not to do when creating your own exceptions.

From the information provided in this chapter, you will gain a better understanding of how C++ exceptions work under the hood and the types of things that can be done with C++ exceptions to build more robust and reliable C++ programs.

The recipes in this chapter are as follows:

  • Using the noexcept specifier
  • Using the noexcept operator
  • Using RAII
  • Learning why to never throw exceptions in destructors
  • Easily creating your own exception classes