Home > Backend Development > C++ > body text

What is the difference between C++ function error handling and exception handling?

WBOY
Release: 2024-04-24 08:00:01
Original
451 people have browsed it

In C, error handling and exception handling are different mechanisms for handling unexpected situations. Error handling uses the errno global variable or the GetLastError() function to set the error code, and the developer needs to manually check the error. Exception handling throws or captures exception objects, including error information and types, and the compiler automatically handles error propagation and recovery. The main differences include:

C++ 函数错误处理与异常处理有何区别?

The difference between C function error handling and exception handling

In C, error handling and exception handling are Different mechanisms for handling unexpected situations.

Error handling

  • Use the errno global variable (POSIX standard) or the GetLastError() function (Windows API) to set an error code.
  • Developers are responsible for checking error codes and taking appropriate actions.
  • Example:
// 打开文件
FILE* fp = fopen("file.txt", "r");

// 检查错误
if (fp == NULL) {
  int errnum = errno;
  // 根据 errnum 采取适当的措施
}
Copy after login

Exception handling

  • Throw or catch a set of objects called exceptions.
  • The exception object contains information about the error and its type.
  • The compiler automatically handles error propagation and recovery.
  • Example:
class MyException : public exception {
public:
  const char* what() const noexcept {
    return "This is an example exception.";
  }
};

// 抛出一个异常
throw MyException();

// 捕获异常
try {
  // 代码可能抛出异常
} catch (MyException& e) {
  // 处理 MyException 异常
}
Copy after login

Key differences

##Information Code onlyError types and informationReuseNoneCan create custom exception types PerformanceFastSlower
FeaturesError handling Exception handling
ComplexityLowHigh
ControlManual error checking by developers Automatic by compiler
##Practical case: file operation

Consider Functions that use file operations. We can throw the

FileNotFoundException

exception when the file fails to open and handle the exception in the main program.

// 定义文件未找到异常
class FileNotFoundException : public exception {
public:
  const char* what() const noexcept {
    return "File not found.";
  }
};

// 打开文件的函数
void openFile(const char* filename) {
  FILE* fp = fopen(filename, "r");
  if (fp == NULL) {
    throw FileNotFoundException();
  }
}

// 主程序
int main() {
  try {
    openFile("myfile.txt");
  } catch (FileNotFoundException&) {
    cout << "File not found." << endl;
  }
}
Copy after login

The above is the detailed content of What is the difference between C++ function error handling and exception handling?. For more information, please follow other related articles on the PHP Chinese website!

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