Home >Backend Development >C++ >C++ function exceptions and class exceptions: multiple exception handling strategies

C++ function exceptions and class exceptions: multiple exception handling strategies

WBOY
WBOYOriginal
2024-05-03 17:18:01345browse

C Exception handling is divided into two types: function exceptions and class exceptions. Multiple exception handling strategies include handling one by one and capturing base classes. In actual combat, exception handling strategies can be used to handle exceptions from different sources and print different error messages according to the exception type.

C++ 函数异常与类异常:多重异常处理策略

C Function Exceptions and Class Exceptions: Multiple Exception Handling Strategies

Overview

Exception handling is a way to handle runtime errors in C powerful mechanism. It allows programs to respond gracefully and recover when exceptions occur. There are two types of exceptions in C: function exceptions and class exceptions. This article will explore these two exception types and show how to use multiple exception handling strategies to handle them simultaneously.

Function exception

Function exception is an exception thrown directly in the function. They use the throw keyword followed by an exception type or object. For example:

void divide(int a, int b) {
  if (b == 0)
    throw std::runtime_error("Division by zero");
}

Class exception

Class exception is an exception thrown by a user-defined class. They use the throw keyword followed by an exception class instance. For example:

class NegativeNumberException : std::exception {
public:
  const char *what() const override {
    return "Cannot accept negative numbers";
  }
};

void checkNumber(int n) {
  if (n < 0)
    throw NegativeNumberException();
}

Multiple exception handling strategies

Sometimes, a function or class may throw multiple exceptions at the same time. In this case, you can use a multiple exception handling strategy to handle all exceptions. The following strategies are commonly used:

1. Process one by one:

try {
  // 调用可能抛出异常的代码
} catch (std::runtime_error &e) {
  // 处理 std::runtime_error 异常
} catch (NegativeNumberException &e) {
  // 处理 NegativeNumberException 异常
} catch (...) {
  // 处理任何其他异常
}

2. Capture the base class:

try {
  // 调用可能抛出异常的代码
} catch (std::exception &e) {
  // 处理所有 std::exception 派生的异常
} catch (...) {
  // 处理任何其他异常
}

Actual combat Case

Consider the following code, which calls the divide and checkNumber functions and prints messages based on different exception conditions:

#include <iostream>
using namespace std;

void divide(int a, int b);
void checkNumber(int n);

int main() {
  try {
    divide(5, 0);
  } catch (std::runtime_error &e) {
    cout << "Division by zero occurred" << endl;
  } catch (NegativeNumberException &e) {
    cout << "Negative number detected" << endl;
  } catch (...) {
    cout << "Unknown exception occurred" << endl;
  }

  try {
    checkNumber(10);
  } catch (...) {
    cout << "Error in number check" << endl;
  }

  return 0;
}

Output:

Division by zero occurred
Error in number check

The above is the detailed content of C++ function exceptions and class exceptions: multiple exception handling strategies. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn