Home > Backend Development > C++ > How does C++ exception handling support custom error handling routines?

How does C++ exception handling support custom error handling routines?

王林
Release: 2024-06-05 12:13:56
Original
658 people have browsed it

C++ Exception handling allows the creation of custom error handling routines to handle runtime errors by throwing exceptions and catching them using try-catch blocks. 1. Create a custom exception class derived from the exception class and override the what() method; 2. Use the throw keyword to throw an exception; 3. Use the try-catch block to catch exceptions and specify the exception types that can be handled.

C++ 异常处理如何支持自定义错误处理例程?

C++ Exception Handling: Support for custom error handling routines

In C++, exception handling is a process that handles runtime A powerful mechanism for errors. It allows you to create custom error handling routines to handle error situations in an elegant and efficient manner.

Exception class

In C++, exceptions are represented by the exception class or its derived classes. To throw a custom exception, create your own derived class and override the what() method. This method returns a string describing the error.

class MyCustomException : public std::exception {
public:
  const char* what() const noexcept override {
    return "This is my custom exception.";
  }
};
Copy after login

Throw an exception

Use the throw keyword to throw an exception. It accepts an exception object as parameter:

throw MyCustomException();
Copy after login

Catch the exception

Catch the exception using the try-catch block. Each catch clause specifies an exception type that can be handled. If an exception of matching type occurs, the code in this clause will be executed:

try {
  // 可能抛出异常的代码
} catch (MyCustomException& e) {
  // 处理 MyCustomException 异常
} catch (std::exception& e) {
  // 处理所有其他类型的异常
}
Copy after login

Practical Case

Let us consider a file that is opened and read from it function. If the file cannot be opened, the function should throw our custom exception:

#include <fstream>
#include <iostream>

using namespace std;

// 自定义异常类
class FileOpenException : public std::exception {
public:
  const char* what() const noexcept override {
    return "Could not open the file.";
  }
};

// 打开文件并读取其内容的函数
string read_file(const string& filename) {
  ifstream file(filename);
  if (!file.is_open()) {
    throw FileOpenException();
  }

  string contents;
  string line;
  while (getline(file, line)) {
    contents += line + '\n';
  }
  file.close();

  return contents;
}

int main() {
  try {
    string contents = read_file("file.txt");
    cout << contents << endl;
  } catch (FileOpenException& e) {
    cout << "Error: " << e.what() << endl;
  } catch (std::exception& e) {
    cout << "An unexpected error occurred." << endl;
  }
  
  return 0;
}
Copy after login

In the above example, the read_file() function throws the FileOpenException exception, Launched when a file cannot be opened. In main() function we use try-catch block to catch exceptions and handle them accordingly.

The above is the detailed content of How does C++ exception handling support custom error handling routines?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template