Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced C++ Programming Cookbook

You're reading from   Advanced C++ Programming Cookbook Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction

Arrow left icon
Product type Paperback
Published in Jan 2020
Publisher Packt
ISBN-13 9781838559915
Length 454 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dr. Rian Quinn Dr. Rian Quinn
Author Profile Icon Dr. Rian Quinn
Dr. Rian Quinn
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with Library Development 2. Using Exceptions for Error Handling FREE CHAPTER 3. Implementing Move Semantics 4. Using Templates for Generic Programming 5. Concurrency and Synchronization 6. Optimizing Your Code for Performance 7. Debugging and Testing 8. Creating and Implementing Your Own Container 9. Exploring Type Erasure 10. An In-Depth Look at Dynamic Allocation 11. Common Patterns in C++ 12. A Closer Look at Type Deduction 13. Bonus - Using C++20 Features 14. Other Books You May Enjoy

Easily creating your own exception classes

In this recipe, you will learn how to easily create your own exception types. This is an important lesson to learn as, although C++ exceptions are easy to create yourself, some guidelines should be followed to ensure this is done safely.

Getting ready

Before beginning, please ensure that all of the technical requirements are met, including installing Ubuntu 18.04 or higher and running the following in a Terminal window:

> sudo apt-get install build-essential git cmake

This will ensure your operating system has the proper tools to compile and execute the examples in this recipe. Once this is complete, open a new Terminal. We will use this Terminal to download, compile, and run our examples.

How to do it...

Perform the following steps to try the recipe:

  1. From a new Terminal, run the following to download the source code:
> cd ~/
> git clone https://github.com/PacktPublishing/Advanced-CPP-CookBook.git
> cd Advanced-CPP-CookBook/chapter02
  1. To compile the source code, run the following:
> mkdir build && cd build
> cmake ..
> make recipe05_examples
  1. Once the source code is compiled, you can execute each example in this recipe by running the following commands:
> ./recipe05_example01
The answer is: 42

> ./recipe05_example02
The answer is: 42

> ./recipe05_example03
The answer is: 42

In the next section, we will step through each of these examples and explain what each example program does and how it relates to the lessons being taught in this recipe.

How it works...

Creating your own C++ exceptions allows you to filter out what type of exception you are getting. For example, did the exception come from your code or the C++ library? By creating your own C++ exceptions, you can easily answer these questions during runtime in your own code. Let's look at the following example:

#include <iostream>
#include <stdexcept>

class the_answer : public std::exception
{
public:
the_answer() = default;
const char *what() const noexcept
{
return "The answer is: 42";
}
};

int main(void)
{
try {
throw the_answer{};
}
catch (const std::exception &e) {
std::cout << e.what() << '\n';
}
}

As shown in the preceding example, we create our own C++ exception by inheriting std::exception. This is not a requirement. Technically, anything can be a C++ exception including an integer. Starting from std::exception, however, gives you a standard interface to work from including overriding the what() function, which describes the exception that was thrown.

In this preceding example, we return a hardcoded string in the what() function. This is the ideal type of exception (even more so than the exceptions that are provided by the C++ library). This is because this type of exception is nothrow copy-constructable. Specifically, this means that the exception itself can be copied without the copy generating an exception, for example, due to std::bad_alloc. The exception types provided by the C++ library support construction from std::string(), which could throw std::bad_alloc.

The issue with the preceding C++ exception is that you would need 1 exception type for every type of message you wish to provide. Another way to implement a safe exception type is to use the following:

#include <iostream>
#include <stdexcept>

class the_answer : public std::exception
{
const char *m_str;
public:

the_answer(const char *str):
m_str{str}
{ }

const char *what() const noexcept
{
return m_str;
}
};

int main(void)
{
try {
throw the_answer("42");
}
catch (const std::exception &e) {
std::cout << "The answer is: " << e.what() << '\n';
}
}

In the preceding example, we store a pointer to const char* (that is, a C-style string). C-style strings are stored globally as constants within the program. This type of exception satisfies all of the same preceding rules, and no allocations are taking place during the construction of the exception. It should also be noted that, since the strings are stored globally, this type of operation is safe.

Many types of exceptions can be created using this approach, including things other than strings that are accessible through custom getters (that is, without having to use the what() function). If, however, these preceding rules are not an issue for you, the easiest way to create a custom C++ exception is to simply subclass an existing C++ exception such as std::runtime_error(), as in the following example:

#include <iostream>
#include <stdexcept>
#include <string.h>

class the_answer : public std::runtime_error
{
public:
explicit the_answer(const char *str) :
std::runtime_error{str}
{ }
};

int main(void)
{
try {
throw the_answer("42");
}
catch (const the_answer &e) {
std::cout << "The answer is: " << e.what() << '\n';
}
catch (const std::exception &e) {
std::cout << "unknown exception: " << e.what() << '\n';
}
}

When this example is executed, we get the following:

In the preceding example, we create our own C++ exception in just a few lines of code by subclassing std::runtime_error(). We can then use different catch blocks to figure out what type of exception was thrown. Just remember that if you use the std::string version of std::runtime_error(), you could end up with std::bad_alloc being thrown during the construction of the exception itself.

You have been reading a chapter from
Advanced C++ Programming Cookbook
Published in: Jan 2020
Publisher: Packt
ISBN-13: 9781838559915
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.
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 $19.99/month. Cancel anytime
Banner background image