Hello! Today, we will learn about throwing exceptions in C++, which is vital for writing robust programs. Think of exceptions like traffic signals. When driving, if you see a red light, you stop to avoid collisions—a clear sign something needs attention. Similarly, exceptions signal problems in our code, allowing us to handle errors smoothly. By the end, you'll know how to throw and catch exceptions, making your programs more resilient.
To "throw" an exception means to signal that something unusual has occurred. In C++, we use the throw keyword to do this. It’s like raising your hand to say, "Wait, there's a problem!"
Here’s a simple example. Imagine you ask a vending machine for a snack that's out of stock. The machine needs to inform you that it can't complete your request:
Output:
Imagine we have a function that can throw an error:
In this case, everything is fine, and the function is successfully executed. But if we call it like this, it will throw an error:
This way, our function is designed to warn the program with an error that something went wrong. However, we don't want the program to terminate every time this error appears. Let's learn how to handle it.
In C++, we can handle exceptions with a special try-catch block. The program tries to execute the code inside the try, and in case it fails with an exception, executes the code inside the catch.
Let’s modify our example:
When the give_order function is called with order = 5 and stock = 3, it will throw a std::runtime_error because the order exceeds the stock.
The catch block catches exceptions of type std::runtime_error. If the exception is caught, it prints the error message using e.what() where e is the exception object caught. This prevents the program from crashing and allows you to handle the error however you want.
This way, your program continues to run and provides a meaningful handling of an error.
Sometimes, different types of exceptions can occur, and it’s essential to handle each type uniquely. In C++, you can catch multiple exceptions by adding multiple catch blocks. Each catch block handles a specific type of exception.
Here’s a meaningful real-life example. Imagine a bank application where you need to process a transaction. Different errors can occur during the process, such as insufficient funds or an invalid account:
The process_transaction function can throw either a std::invalid_argument if the account is invalid or a std::runtime_error if there are insufficient funds.
In this code:
- In the
mainfunction, we try to callprocess_transactionwithaccount_valid = true. - If a
std::invalid_argumentis thrown, it is caught by the firstcatchblock. - If a
std::runtime_erroris thrown, it is caught by the secondcatchblock.
This method allows you to handle different types of exceptions separately, providing more specific error management in your programs. In this case, the program will execute successfully and print out "Transaction processed successfully!". Let's look at other options.
Firstly, it is possible that the account is not valid:
In this case, the first catch block works.
Secondly, it is possible that there are not enough funds on the balance:
In this case, the second catch block works.
Lastly, there can be the both problems at once:
In this case, the first catch block will work as it is the first one. The second catch block won't be executed.
Great job! Today, you've learned how to throw and catch exceptions in C++. Just like having a good set of tools helps you fix things, knowing how to handle exceptions helps you fix problems in your code more smoothly.
To recap:
- Throwing exceptions signals that something has gone wrong, using the
throwkeyword. - Catching exceptions with
tryandcatchblocks helps you manage these errors.
Now it’s time to put what you’ve learned into practice! You’ll move to hands-on coding exercises where you’ll throw, catch, and handle exceptions, making your programs more robust. Happy coding!
