Throwing and Handling an out_of_range Exception 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 this section, we've been learning a lot about exception handling and debugging. In this project lecture, you will create a simple function named getLetterGrade. It will take an integer that represents a percentage and return a character representing the letter grade that the percentage corresponds to. If the calling code passes in a value less than zero or greater than 100, it will throw an out of range exception. Store the message, your percent must be within zero and 100 inclusive. Make sure to remember to include the stdexcept library. You should also catch this exception with a try catch structure in main. You can see the list of percent ranges and grades on the slide. So, let's run this great project before we actually do it. So, it looks something like this. You will get grades printed out in main that were returned from our getLetterGrade function and this right here your percent must be within zero and 100 inclusive that was caused by the exception. So, that was the exception text. So, hopefully that helps. So, pause the video and create a project called GradeProject and give this your best shot. Come back when you're done or if you need some help. How did the project work out for you? Were you able to complete it? Let's do it together. So, we'll create a project here. Empty project, Next, and we'll call this GradeProject. We need to create a main file here, add new item and we'll call it main.cpp. We will add the skeleton, we need to make sure we have stdexcept though. So, we need a getLetterGrade that takes a percent and returns a LetterGrade. Awesome. And then we'll fill in main of course. But, let's start with getLetterGrade. So, char grade and eventually we want to return the grade. If the percent is less than zero or the percent is greater than 100, and we use OR here because it can't be both at the same time. Because if it's less than zero or greater than 100. So, if you put AND here, that would never be true because the percent could never be both less than zero and greater than 100 because it would be negative if it's less than zero. If this is the case though, we throw an out of range exception that says, your percent must be within and 100 inclusive. So, else if percent is greater than or equal to 90, that's the grade equals A. So, that would be anything above 90. But we know it will be less than 100 because we take care of that here. All right, we aren't explicitly writing the ranges, but we are implicitly taking care of them by writing them in the correct order when we create our if else structure. All right, percent greater than or equal to 80, then the grade is A, B. All right, so far so good. If the percent is greater than or equal to 70 and it gets to this point that means it wasn't less than zero or greater than 100. It wasn't greater than or equal to 90. It wasn't greater than or equal to 80. So, the only thing it could be if it's an integer is 70 all the way up to 79, which is part of the range. That's the range that we wanted. We'll set grade = C, then else if percent greater than or equal to 60, grade = D. And then finally we have the default value which is given by an else. The only other possibility is the grade has to be an F. So, you have to be doing really bad to get a G apparently. So, now let's fill in main. So, we have to fill in main to actually test this out. So, at the top of main we will create a letter to hold the LetterGrade. char letter, we have try and we have catch and it catches an out of range exception. So far so good. Nice and clean. In catch all we have to do is say err.what. All right. In try... we will say getLetterGrade on, say 99%. And then cout LetterGrade. And then what about letter equals getLetterGrade on 50%. Print that out and then letter equals getLetterGrade and now we're going to try to crash it by using -5%. All right, now let's run it. Debug, start without debugging and there we go. We get an A for the 99% and F for the 50. And then we get the exception, 'your percent must be within 0 and 100 inclusive' and that's what is an err.what. Nice. So, the calls that have valid grade percentages work simply fine. But when we pass an invalid grade to the getLetterGrade function, an exception is thrown by the function and caught in the catch block inside of main. Good job, everyone. In the next lecture, you will work on another project where you'll use your own custom exception. Let's get going.


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