Creating and Using Your Own Exceptions Project

The course is part of this learning path

Start course
1h 51m

In this course, we will discuss the topic of exceptions and debugging. We'll see that exceptions are objects representing exceptional situations and that these are usually problems in our code or in the environment in which our code is running. By using exception handling, we will know how to respond to problems when they arise.

Learning Objectives

  • Learn the proper syntax and techniques to work with exceptions
  • Understand inheritance in the context of Object-Oriented Programming

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++


To get the most out of this course, you should have a basic understanding of the fundamentals of C++.



In the last lecture we did a project related to throwing and catching a built-in exception, the out of range exception. In this lecture, we will create a new project, SpaceshipProject, and you will be able to demonstrate your exceptional abilities. Get it, exception. Okay, never mind. Anyway, you'll demonstrate your abilities to create a custom exception class named WarpDriveOverheating. Your ship's warp drive used for interstellar travel requires the drive enclosure to remain at a temperature lower than 80°C. The captain has requested that you write a small program to monitor the temperatures of the warp drive enclosure and that an alert should be printed to standard output if the warp drive exceeds the 80°C. So, you will create a custom WarpDriveOverheating exception with the default message, "Warp drive has exceeded safe temperature tolerance." Only write a single constructor. Your class should extend overflow error. Remember that you only need to implement the constructor with an initializer list and overflow error will take care of the rest. You should of course run a few tests in main as well. You can keep it simple and just create a warpTest function in the main file and call it multiple times with different temperatures. Make sure to test both valid temperatures and those over the 80°C. If the temperatures remain 80° or under, then you could just print out temperatures within tolerance. You have some flexibility with your implementation, of course, just try to adhere to the requirements. Also, make sure to remember to include the stdexcept library. All right, here's an example of what it looks like when the SpaceshipProject is run. You'll notice I have the WarpDriveOverheating.h and main.cpp over here. I put the entire exception inside of WarpDriveOverheating.h. And of course I'm not going to show you the code yet because I hope you'll be able to give it right. The main.cpp, there's a lot of flexibility with that. If I run it right here, it should probably look like something like this. I used the loop, you don't have to. I just used the loop and added i times something in that loop in a for loop, but you can just make some manual calls if you want to. You can use a loop, whatever you want to do. I put testing outside the loop. Loop, loop, loop, loop, loop. And then when it gets to the point where it's over or over the 80 degrees, then it says 'warp drive has exceeded safe tolerance'. So, hopefully that was useful. So, pause the video, create the SpaceshipProject in visual studio and work on this project. When you're done or if you just want to see my solution, come back and we'll work on it together. How was that project for you? Did you find it relatively easy or difficult? Let's work on it together. So, we're going to create the SpaceshipProject. Empty project, and then we call it SpaceshipProject. Hit 'Creates' and of course I'm going to create a main file and we could get this set up also with a WarpDriveOverheating file. So, we'll keep everything in the .h file. So, the .cpp could be used but it's not really necessary. WarpDriveOverheating.h. Great. main.cpp, we're going to put include iostream, include standard exception and also of course we need to include the WarpDriveOverheating. All right, good. Now the WarpDriveOverheating.h ifndef. I'm not going to write out a huge name here or the full name, I'm just going to put WARPDRIVE_H because it doesn't have to match it. We don't have to identically match it, match the name of the class. Just as long as these are consistent and have something to do with the name, it's usually sufficient. So, in here stdexcept using namespace std and class WarpDriveOverheating inherits using public inheritance from overflow error and then just has a public section and this uses the initializer, or initialization list with overflow_error. "Warp drive has exceeded safe temperature tolerance." Now we need a body but it will be empty. So, an empty body on that one. Now we need to go over and fill in the main.cpp file. So, I want to create a function called warpTest that takes a temperature and also fill it in down here. Now what are we going to do in here? If the temp is less than or equal to 80 degrees, we print out temperature is within tolerance else throw WarpDriveOverheating. We don't have to pass on a message. We don't have a constructor that takes a message, that should be overflow_error, in case I goofed. So, we have to make sure it says overflow_error, and it's going to throw the WarpDriveOverheating exception. And now we have to actually call this in main and set up some try catch. So, again, there was some flexibility here. So, don't worry if yours doesn't look identical, it's very unlikely it's going to be identical. We've got catch const WarpDriveOverheating, reference and we're going to print out the err.what. And then in the try, I'm going to try some different temperatures. Now, here's my choice. You could have done it manually, it's totally fine. But for mine, I'm testing out, starting with 50°. I'm doing i * 10 and adding that to the 50. So, we'll start with 50° and then the first round it will be 50°, then 60°, and 70 all the way up, up, and up. So, once it gets to the point where it goes above the 80 degree mark, it's going to cause the exception to occur. So, all right, because warpTest will throw it. It'll jump down here and then print out our exceptions. All right, pretty good. Let's run it and see what happens. Debug, start without debugging, and you'll notice testing was printed here and then for a few of these it's fine, temperature's within tolerance. And then after a while, the exception has caught, warp drive has exceeded safe temperature tolerance. Excellent, good job, everyone. I hope you found that program very interesting and that it helped you solidify your skills in understanding of exceptions. In the next lecture, we will wrap up this section. I'll see you there.

About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics