---
title: "How exceptions can be caught and re-thrown to be handled by higher-level functions?"  
description: "How exceptions can be caught and re-thrown to be handled by higher-level functions?"  
author: "Revati S Misra"  
published: 2023-08-16  
updated: 2023-08-17  
canonical: https://www.mindstick.com/forum/159541/how-exceptions-can-be-caught-and-re-thrown-to-be-handled-by-higher-level-functions  
category: "visual c++"  
tags: ["exception handling", "exception", "c++"]  
reading_time: 2 minutes  

---

# How exceptions can be caught and re-thrown to be handled by higher-level functions?

How [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) can be caught and re-thrown to be handled by higher-level [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing)?

## Replies

### Reply by Aryan Kumar

Exceptions can be caught and re-thrown to be handled by higher level functions in C++ using the `throw` keyword. The `throw` keyword is used to throw an exception. The exception can be any object, but it is usually a `std::exception` object or a derived class of `std::exception`.

To catch and re-throw an exception, you can use the `catch` statement. The `catch` statement specifies the type of exception that you want to catch. If an exception of the specified type is thrown, the `catch` statement will be executed.

The following code shows how to catch and re-throw an exception:

C++

```plaintext
#include <iostream>
#include <exception>

using namespace std;

void foo() {
  throw std::runtime_error("An error occurred");
}

void bar() {
  try {
    foo();
  } catch (std::runtime_error e) {
    // Re-throw the exception
    throw e;
  }
}

int main() {
  try {
    bar();
  } catch (std::runtime_error e) {
    cout << "An error occurred: " << e.what() << endl;
  }

  return 0;
}
```

In this code, the `foo()` function throws a `std::runtime_error` exception. The `bar()` function catches this exception and then re-throws it. The `main()` function catches the exception and prints a message to the console.

It is important to note that the `re-thrown` exception will be caught by the first `catch` statement that matches its type. If there is no `catch` statement that matches the type of the exception, the program will terminate.

Here are some additional things to keep in mind when catching and re-throwing exceptions:

- You can catch multiple exceptions in a single `catch` statement by using the `|` operator. For example, the following code catches both `std::runtime_error` and `std::logic_error` exceptions:

C++

```plaintext
catch (std::runtime_error | std::logic_error e) {
```

You can also catch a wildcard exception by using the `catch (...)` statement. The `catch (...)` statement will catch any exception, regardless of its type.

It is important to handle all exceptions that are thrown by your code. If an exception is not handled, the program will terminate.


---

Original Source: https://www.mindstick.com/forum/159541/how-exceptions-can-be-caught-and-re-thrown-to-be-handled-by-higher-level-functions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
