Introduction - Error Handling
Error handling is necessary for any computer program. (Literally any.) The way C++ programs typically handle C++ is through exceptions.
The way to think about it is that a program has several layers of code, each doing something. For example, the top layer is main, which is responsible for running the entire programming, and maybe main calls some other functions to do stuff, and those functions call other functions. What happens if a function several layers deep encounters an unexpected problem, or has some problem and needs advice on what to do? The answer, with exceptions, is that it throws the problem back up to it's previous layer, and that previous layer has to decide what to do. C++ Try-Catch Blocks
Try-Catch blocks are the basic mechanism used to handle exception handling, so let's look at a very basic C++ try-catch example.
There are a few things to note:
This is the general idea of try-catch blocks: they're really just control statements that help our program flow as we want it too. But we need to know a few exception types that we actually need to watch out for (otherwise, we could just handle everything with if statements). Let's look at a few.
Exceptions
We threw int and string before, but there are more useful types to throw to really be descriptive about what the error is. There are quite a few C++ exception types, and cppreference.com has created a very nice list.
Let's go over one specific example to see how this generally works.
Output:
Program started. Inside the function. Stop trying to access an invalid index! Program ended.
What we do it use normal code to check whether an error will occur or not, and if so, we throw that type of exception, which gets handled by the calling function.
Conclusion
Exceptions are an important part of any program. If we don't use exceptions, then our programs will crash, or we'll get undefined behavior.
Using exceptions is pretty simple, but exceptions should be thought about at the design level, and not just in the implementation level. (Meaning, you need to think about errors and exceptions when you first make your codebase.) To see some more advanced C++ exception tips, watch John Kalb's exception presentation on youtube.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Introduction - RAII
RAII (Resource Acquisition is Initialization) is a programming concept in C++. We're going to describe it very simply:
Problem: Memory leaks occur when heap allocated (new keyword) variables/objects don't get deleted because of unexpected problems. For example...
The above code has a memory leak, because a resource is initialized, but is never deleted. This is where RAII comes in...
Solution: Encapsulate variables in classes, and design the destructors to deallocate the resource from memory when the object itself is deleted.
The reason this works is because the object will have it's destructor called when it's scope ends (for example, when the throw happens). The best way to make this happen is with smart pointers. There are a few types, but we'll look at two in this blog post. Unique Pointers
Unique pointers are a good RAII technique for objects you only want to have one pointer too. (Meaning, only one pointer is allowed access to the variable/object.)
The output of the above code (compiled with c++14) is:
Program started. Shared Pointers
Shared Pointer is the same as unique pointers, except there can be multiple pointers to one variable/object.
The code will output the following:
Program started. Conclusion
RAII is an important C++ technique, where the code is designed so that even unexpected code breaking will assure that objects are cleaned up and memory leaks don't occur. We've seen how Smart Pointers, such as unique pointer and shared pointer, allow us to automatically handle RAII, but RAII is a design principle that can be applied to even our custom classes where any type of resource that we're responsible for needs to be designed carefully.
To explain it simply: Put a resource in the constructor of a class. Clean up that resource in the destructor of that class. And use a smart pointer to handle using that class. EZ.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References
1. www.fluentcpp.com/2017/08/22/smart-developers-use-smart-pointers-smart-pointers-basics/ 2. www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/ 3. stackoverflow.com/questions/8480640/how-to-throw-a-c-exception 4. www.youtube.com/watch?v=ENj37HvptgU 5. stackoverflow.com/questions/22571202/differences-between-stdmake-unique-and-stdunique-ptr-with-new |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|