c/c++ 异常处理

 

c++ 提供了一套强大的异常处理机制, 用于管理程序运行过程中发生的错误和意外情况

异常处理机制通过 try、catch 和 throw 关键词来实现, 旨在帮助开发者捕捉程序中的异常并进行有效的错误处理

同步异常

同步异常是指程序内部产生的错误, 通常是在程序运行过程中由操作引发的异常

最常见的同步异常是运行时错误, 例如除零错误、数组越界等

同步异常通常是在函数内部触发, 并且可以通过 throw 语句显式抛出

  • 示例, 除零异常
#include <iostream>
#include <stdexcept>

void divide(int a, int b) {
    if (b == 0) {
        throw std::runtime_error("division by zero!");
    }
    std::cout << "result: " << a / b << std::endl;
}

int main() {
    try {
        divide(10, 0);
    } catch (const std::runtime_error& e) {
        std::cerr << "error: " << e.what() << std::endl;
    }
    return 0;
}

运行结果

Error: division by zero!

示例中, 通过 throw 抛出了一个运行时错误, catch 语句捕获了异常并打印了错误信息

通过异常处理机制, 能够优雅地处理这种异常, 避免程序崩溃

异步异常

异步异常通常由外部事件引起, 例如硬件错误、操作系统发出的信号或其他外部因素

在实际编程中, 异步异常的发生通常较为少见, 但是在涉及到文件操作、网络通信等外部输入输出时, 常常需要处理这类异常

  • 示例, 文件未找到异常
#include <iostream>
#include <fstream>

// 包含std::runtime_error头文件
#include <stdexcept>

void open_file(const std::string& filename) {
    std::ifstream file(filename);
    if (!file) {
        throw std::runtime_error("File not found!");
    }
}

int main() {
    try {
        open_file("nonexistent.txt");
    } catch (const std::runtime_error& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return 0;
}

运行结果

Error: File not found!
  • 示例, 输入验证异常

输入字符串, 若其为数字且小于10000且为偶数则为合法状态, 其他为非法状态

#include <iostream>
#include <cmath>

bool judge_num(std::string s) {
    if (s.size() > 5) {
        throw "长度超长";
    }

    int sum = 0;
    for (int i = 0, size = s.size(); i < size; i++) {
        int v = s[i] - '0';
        if (v < 0 || v >9) {
            throw "该字符串非数字";
        }
        sum += (v * pow(10, size - i - 1));
    }

    if (sum > 10000) {
        throw "数字值大于10000";
    }
    if (sum & 1) {
        throw "该数字不为偶数";
    }
    return true;
}

int main() {
    std::string s = "1";
    try {
        if (judge_num(s)) {
            std::cout << s << "是一个合法数字字符串" << std::endl;
        }
    } catch (const char* msg) {
        std::cout << s << "不是一个合法数字字符串" << std::endl;
        std::cerr << "错误原因:" << msg << std::endl;
    }
    return 0;
}

运行结果

1001 is not a valid number string.
Error: The number is not an even number
  • 示例, 除法异常

在除法运算中, 如果遇到除数为 0 的情况, 需要抛出异常并进行处理

#include <iostream>
#include <cstdlib>

double divide(double x, double y) {
    if (y == 0) {
        // 除数为0, 抛出异常
        throw y;
    }
    return x / y;
}

int main() {
    double res;
    try {
        res = divide(2, 3);
        // The result of x/y is:0.666667
        std::cout << "The result of x/y is:" << res << std::endl;

        res = divide(4, 0);
    } catch (double) {
        // Error of dividing zero.
        std::cerr << "Error of dividing zero." << std::endl;
        exit(1);
    }
    return 0;
}

运行显示

The result of x/y is:0.666667
Error of dividing zero.