当前位置: 首页> 财经> 访谈 > 推广软件下载平台_上海房地产信息网官网_关键词优化难度查询_网页设计需要学什么

推广软件下载平台_上海房地产信息网官网_关键词优化难度查询_网页设计需要学什么

时间:2025/7/13 6:19:08来源:https://blog.csdn.net/TsuanS13/article/details/147011446 浏览次数:1次
推广软件下载平台_上海房地产信息网官网_关键词优化难度查询_网页设计需要学什么

C++ 异常处理概述

C++ 异常处理机制提供了一种在程序运行时捕获错误或异常情况的方式。异常处理的目的是使得程序在遇到错误时能够优雅地终止或恢复,并防止程序出现崩溃。C++ 使用 try, throw, 和 catch 关键字来实现异常处理。

异常处理的基本结构:

  1. throw: 用来抛出异常,可以是任意类型的对象。

  2. try: 用来包裹可能抛出异常的代码块。

  3. catch: 用来捕获特定的异常类型并处理。

1. throw 关键字

throw 用于抛出异常。当程序执行到 throw 时,会跳出当前的代码块,异常被抛出,程序开始寻找相应的 catch 语句。

  • 语法throw expression;

    • expression 可以是任何数据类型,通常是一个异常对象。

    • 可以抛出内建类型、类对象、指针等。

throw 42;  // 抛出整数 42
throw std::runtime_error("An error occurred");  // 抛出异常对象

2. try

try 块是包含可能会抛出异常的代码的区域。当 try 块中的代码抛出异常时,控制权会跳转到相应的 catch 块中。

  • 语法

    try {// 可能抛出异常的代码
    }
    

3. catch

catch 用于捕获异常,并且可以处理它。catch 块的形式取决于抛出的异常类型。如果异常类型匹配,catch 语句就会捕获它。

  • 语法

    catch (ExceptionType e) {// 处理异常
    }
    
    • 你可以捕获特定的异常类型,也可以使用通用的 catch(...) 来捕获任何类型的异常。

基本的异常处理示例:

#include <iostream>
#include <stdexcept>  // std::runtime_errorvoid testFunction() {throw std::runtime_error("An error occurred");
}int main() {try {testFunction();  // 可能抛出异常的函数}catch (const std::exception& e) {  // 捕获异常std::cout << "Caught exception: " << e.what() << std::endl;}return 0;
}

详细讲解:

1. 抛出异常

try 块中,可能会发生错误。此时,你可以使用 throw 语句来抛出异常对象。你可以抛出任何类型的对象,常见的类型包括:

  • 基本数据类型(如 int, double

  • 类或结构体对象

  • 标准库异常类(如 std::exception, std::runtime_error

例子:

throw std::out_of_range("Index out of range");  // 抛出标准异常对象

2. 捕获异常

catch 块会根据其类型匹配捕获不同的异常对象。你可以为不同的异常类型设置多个 catch 块。异常处理机制是基于异常类型进行匹配的,匹配成功的 catch 块会执行。

try {// 代码可能抛出不同类型的异常throw 42;
}
catch (int e) {  // 捕获 int 类型异常std::cout << "Caught an integer: " << e << std::endl;
}
catch (std::exception& e) {  // 捕获所有继承自 std::exception 的异常std::cout << "Caught exception: " << e.what() << std::endl;
}

3. 通用的异常捕获

如果你不确定抛出的异常类型,可以使用一个通用的 catch(...) 来捕获所有未明确捕获的异常。

try {// 代码可能抛出不同类型的异常throw "Something went wrong";
}
catch (...) {  // 捕获所有类型的异常std::cout << "Caught an unknown exception" << std::endl;
}

4. 异常传递

当一个异常被抛出后,程序会按照“栈展开”的方式逐级寻找匹配的 catch 块。找不到时,程序会终止。

例如,如果 main() 中的 catch 无法捕获异常,异常将会沿着调用栈传递,直到程序终止。

5. 多重异常处理

你可以使用多个 catch 块来捕获不同类型的异常,确保程序对各种错误情况作出处理。

try {throw std::out_of_range("Out of range error");
}
catch (const std::out_of_range& e) {std::cout << "Caught out_of_range: " << e.what() << std::endl;
}
catch (const std::exception& e) {std::cout << "Caught exception: " << e.what() << std::endl;
}
catch (...) {std::cout << "Caught unknown exception" << std::endl;
}

6. 异常的传播

C++ 允许在多个函数中抛出异常。当函数内的代码抛出异常时,控制权会跳转到调用该函数的地方。如果异常未被捕获,则程序会终止。

void funcA() {throw std::runtime_error("Error in funcA");
}void funcB() {funcA();  // 异常从 funcA 传播到 funcB
}int main() {try {funcB();}catch (const std::exception& e) {std::cout << "Caught in main: " << e.what() << std::endl;}
}

7. 自定义异常

你可以定义自定义的异常类型,通过继承 std::exception 或其他异常类来创建自己的异常类。

class MyException : public std::exception {
public:const char* what() const noexcept override {return "My custom exception occurred";}
};void test() {throw MyException();  // 抛出自定义异常
}int main() {try {test();}catch (const MyException& e) {std::cout << "Caught custom exception: " << e.what() << std::endl;}
}

C++ 标准库异常类概述

C++ 标准库定义了一些通用的异常类,它们位于 <stdexcept> 头文件中。这些异常类帮助程序员识别和处理不同类型的错误。C++ 标准库异常类通常都继承自 std::exception,这是所有标准异常类的基类。

1. std::exception

std::exception 是 C++ 标准库异常体系的基类。它提供了一个虚函数 what(),返回一个指向常量字符数组的指针,用于描述异常的相关信息。

  • 定义

    class exception {
    public:virtual const char* what() const noexcept;
    };
    
    • what() 方法返回异常信息的描述。

    • noexcept 说明该函数不会抛出异常。

std::exception 是所有标准异常类的父类。如果你创建自定义的异常类,可以继承 std::exception 来提供异常描述。

示例

#include <iostream>
#include <stdexcept>int main() {try {throw std::exception();}catch (const std::exception& e) {std::cout << "Caught exception: " << e.what() << std::endl;}return 0;
}

输出:

Caught exception: 

2. std::runtime_error

std::runtime_error 继承自 std::exception,表示程序在运行时遇到的错误,通常是不可预料的错误。它的构造函数接受一个描述错误的字符串,该字符串将通过 what() 方法返回。

  • 定义

    class runtime_error : public exception {
    public:explicit runtime_error(const char* what_arg);explicit runtime_error(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>int main() {try {throw std::runtime_error("Runtime error occurred!");}catch (const std::runtime_error& e) {std::cout << "Caught runtime_error: " << e.what() << std::endl;}return 0;
}

输出:

Caught runtime_error: Runtime error occurred!

std::runtime_error 通常用于处理程序在运行时出现的异常情况,例如文件操作失败、网络连接错误等。

3. std::logic_error

std::logic_error 也继承自 std::exception,表示程序中的逻辑错误。这些错误通常是程序设计或实现上的错误,通常是程序员在编写程序时犯的错误。常见的逻辑错误包括访问无效的索引、非法的参数传递等。

  • 定义

    class logic_error : public exception {
    public:explicit logic_error(const char* what_arg);explicit logic_error(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>void test(int x) {if (x < 0) {throw std::logic_error("Negative value passed");}
}int main() {try {test(-1);  // 将抛出逻辑错误}catch (const std::logic_error& e) {std::cout << "Caught logic_error: " << e.what() << std::endl;}return 0;
}

输出:

Caught logic_error: Negative value passed

std::logic_error 通常用于处理程序在设计上就不应该发生的错误。

4. std::out_of_range

std::out_of_range 继承自 std::logic_error,用于表示访问超出有效范围的错误。通常用于数组、容器、指针等操作超出有效范围时。

  • 定义

    class out_of_range : public logic_error {
    public:explicit out_of_range(const char* what_arg);explicit out_of_range(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>
#include <vector>int main() {std::vector<int> vec = {1, 2, 3};try {std::cout << vec.at(5) << std::endl;  // 访问越界元素}catch (const std::out_of_range& e) {std::cout << "Caught out_of_range: " << e.what() << std::endl;}return 0;
}

输出:

Caught out_of_range: vector::_M_range_check

std::out_of_range 常用于检查容器操作时索引越界的情况。

5. std::invalid_argument

std::invalid_argument 继承自 std::logic_error,用于表示函数或操作接收到一个非法的参数。这通常是当函数的参数不符合预期时抛出的异常。

  • 定义

    class invalid_argument : public logic_error {
    public:explicit invalid_argument(const char* what_arg);explicit invalid_argument(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>void test(int x) {if (x <= 0) {throw std::invalid_argument("Argument must be positive");}
}int main() {try {test(0);  // 传递非法参数}catch (const std::invalid_argument& e) {std::cout << "Caught invalid_argument: " << e.what() << std::endl;}return 0;
}

输出:

Caught invalid_argument: Argument must be positive

std::invalid_argument 通常用于检测函数参数的合法性。

6. std::length_error

std::length_error 继承自 std::logic_error,用于表示容器或其他数据结构在处理时超过了其最大容量。比如在向一个固定大小的容器中插入元素时,超出了容器的最大长度。

  • 定义

    class length_error : public logic_error {
    public:explicit length_error(const char* what_arg);explicit length_error(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>
#include <vector>int main() {try {std::vector<int> vec(10);vec.resize(100);  // 超出容器允许的长度}catch (const std::length_error& e) {std::cout << "Caught length_error: " << e.what() << std::endl;}return 0;
}

输出:

Caught length_error: vector::resize

std::length_error 用于指示容器长度超过限制。

7. std::overflow_error

std::overflow_error 继承自 std::runtime_error,表示算术操作发生溢出(如整数加法超出了可表示的范围)。这种错误通常发生在数值计算时。

  • 定义

    class overflow_error : public runtime_error {
    public:explicit overflow_error(const char* what_arg);explicit overflow_error(const std::string& what_arg);
    };
    

示例

#include <iostream>
#include <stdexcept>int main() {try {int a = 2147483647;  // 假设int是32位,最大值为2147483647a = a + 1;  // 发生溢出throw std::overflow_error("Integer overflow");}catch (const std::overflow_error& e) {std::cout << "Caught overflow_error: " << e.what() << std::endl;}return 0;
}

输出:

Caught overflow_error: Integer overflow

std::overflow_error 用于表示溢出错误。

8. std::underflow_error

std::underflow_error 继承自 std::runtime_error,表示算术操作发生下溢(例如浮点数计算的下溢)。

更多的异常类: C++ 异常处理
在这里插入图片描述

关键字:推广软件下载平台_上海房地产信息网官网_关键词优化难度查询_网页设计需要学什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: