C++ Multiple `catch` Blocks šŸŽÆ

beginner
24 min

C++ Multiple 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! šŸ“

What are Exceptions in C++?

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.

Understanding try, catch, and throw

C++ 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.

Introducing Multiple 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.

Syntax and Examples

Here's the syntax for multiple catch blocks:

cpp
try { // code that might throw an exception } catch(ExceptionType1) { // code to handle ExceptionType1 } catch(ExceptionType2) { // code to handle ExceptionType2 } ...

Example 1: Handling Different Exception Types

cpp
#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.

Example 2: Handling Base and Derived Class Exceptions šŸ’”

cpp
#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.

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the purpose of the `catch` block in C++?

Quick Quiz
Question 1 of 1

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! šŸš€