catch Blocks šÆWelcome back to CodeYourCraft! Today, we're diving into the exciting world of C++ exception handling, specifically focusing on Multiple catch Blocks. This lesson is designed for both beginners and intermediates, so let's get started! š
Before we delve into multiple catch blocks, let's quickly review what exceptions are. In C++, exceptions are used to handle runtime errors that occur during the execution of a program. Instead of a program crashing unexpectedly, exceptions provide a way to handle errors gracefully.
try, catch, and throwC++ exception handling consists of three keywords: try, catch, and throw.
try block: The try block contains the code that might throw an exception.catch block: The catch block catches the exception and provides a way to handle it.throw keyword: The throw keyword is used to create an exception.catch Blocks š”Multiple catch blocks allow us to catch and handle different types of exceptions in a single try block. Each catch block catches a specific type of exception.
Here's the syntax for multiple catch blocks:
try {
// code that might throw an exception
}
catch(ExceptionType1) {
// code to handle ExceptionType1
}
catch(ExceptionType2) {
// code to handle ExceptionType2
}
...#include <iostream>
#include <stdexcept>
void someFunction() {
throw std::runtime_error("An error occurred");
}
void anotherFunction() {
throw std::invalid_argument("Invalid argument passed");
}
int main() {
try {
someFunction();
anotherFunction();
}
catch(std::runtime_error& e) {
std::cerr << "Runtime error: " << e.what() << std::endl;
}
catch(std::invalid_argument& e) {
std::cerr << "Invalid argument: " << e.what() << std::endl;
}
return 0;
}In this example, we're calling two functions (someFunction and anotherFunction) that throw different types of exceptions. The main function catches these exceptions using multiple catch blocks.
#include <iostream>
#include <exception>
#include <string>
class BaseException : public std::exception {
public:
const char* what() const throw() {
return message.c_str();
}
BaseException(const std::string& message) : message(message) {}
private:
std::string message;
};
class DerivedException : public BaseException {
public:
DerivedException(const std::string& message) : BaseException(message) {}
};
void someFunction() {
throw DerivedException("An error occurred");
}
int main() {
try {
someFunction();
}
catch(BaseException& e) {
std::cerr << "Base exception: " << e.what() << std::endl;
}
return 0;
}In this example, we have a base exception class BaseException and a derived exception class DerivedException. The main function calls a function (someFunction) that throws a DerivedException object. Since DerivedException is a derived class of BaseException, we can catch it using a BaseException catch block.
What is the purpose of the `catch` block in C++?
In the given example, why doesn't the `anotherFunction`'s exception get handled?
That's all for today's lesson on C++ Multiple catch Blocks. By now, you should have a good understanding of how to use multiple catch blocks to handle different types of exceptions in your C++ programs. Happy coding! š
Stay tuned for more exciting lessons on C++. Until next time! š