GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — C++ 异常处理库 <stdexcept> | 菜鸟教程
C++ 教程
C++ 教程
C++ 简介
C++ 环境设置
C++ 基本语法
C++ 注释
C++ 数据类型
C++ 变量类型
C++ 变量作用域
C++ 常量
C++ 修饰符类型
C++ 存储类
C++ 运算符
C++ 循环
C++ 判断
C++ 函数
C++ 数字
C++ 数组
C++ 字符串
C++ 指针
C++ 引用
C++ 日期 & 时间
C++ 基本的输入输出
C++ 结构体(struct)
C++ vector 容器
C++ 数据结构
C++ 面向对象C++ 类 & 对象 C++ 继承 C++ 重载运算符和重载函数 C++ 多态 C++ 数据抽象 C++ 数据封装 C++ 接口(抽象类)C++ 高级教程C++ 文件和流 C++ 异常处理 C++ 动态内存 C++ 命名空间 C++ 模板 C++ 预处理器 C++ 信号处理 C++ 多线程 C++ Web 编程C++ 资源库C++ STL 教程 C++ 导入标准库 C++ 标准库 C++ 有用的资源 C++ 实例 C++ 测验 C++ <iostream> C++ <fstream> C++ <sstream> C++ <iomanip> C++<array>
C++ <vector>
C++ <list>
C++ <forward_list>
C++ <deque>
C++ <stack>
C++ <queue>
C++ <priority_queue>
C++ <set>
C++ <unordered_set>
C++ <map>
C++ <unordered_map>
C++ <bitset>
C++ <algorithm>
C++ <iterator>
C++ <functional>
C++ <numeric>
C++ <complex>
C++ <valarray>
C++ <cmath>
C++ <string>
C++ <regex>
C++ <ctime>
C++ <chrono>
C++ <thread>
C++ <mutex>
C++ <condition_variable>
C++ <future>
C++ <atomic>
C++ <type_traits>
C++ <typeinfo>
C++ <exception>
C++ <stdexcept>
C++ <cstdio>
C++ <cstdint>
C++ <memory>
C++ <new>
C++ <utility>
C++ <random>
C++ <locale>
C++ <codecvt>
C++ <cassert>
C++ <cwchar>
C++ <climits>
C++ <cfloat>
C++ <cstdlib>
C++ <numbers>
C++ OpenCV
C++ 异常处理库 <stdexcept>
异常是程序运行时发生的错误,它们可以被捕获并处理,以避免程序的非正常终止。 语法要使用 #include <stdexcept> 然后,你可以使用这些异常类来抛出和捕获异常。例如: throw std::runtime_error("An error occurred");
异常类
实例下面是一个使用 实例
#include <iostream> #include <stdexcept> void riskyFunction(int x) { if (x < 0) { throw std::invalid_argument("Negative value not allowed"); } std::cout << "Processing " << x << std::endl; } int main() { try { riskyFunction(-1); } catch (const std::invalid_argument& e) { std::cerr << "Caught an exception: " << e.what() << std::endl; } return 0; } 输出结果: Caught an exception: Negative value not allowed 在这个示例中, 标准异常类std::exceptionstd::exception 是所有标准异常类的基类。它提供了一个虚函数 what(),用于返回描述异常的字符串。 实例
#include <iostream> #include <exception> int main() { try { throw std::exception(); } catch (const std::exception& e) { std::cout << "Caught exception: " << e.what() << std::endl; } return 0; } std::logic_errorstd::logic_error 派生自 std::exception,表示程序逻辑错误。它是一个抽象基类,通常由派生类来具体化。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::logic_error("Logic error occurred"); } catch (const std::logic_error& e) { std::cout << "Caught logic error: " << e.what() << std::endl; } return 0; } std::invalid_argumentstd::invalid_argument 派生自 std::logic_error,表示传递了无效的参数。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::invalid_argument("Invalid argument provided"); } catch (const std::invalid_argument& e) { std::cout << "Caught invalid argument: " << e.what() << std::endl; } return 0; } std::domain_errorstd::domain_error 派生自 std::logic_error,表示参数超出了有效范围。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::domain_error("Domain error occurred"); } catch (const std::domain_error& e) { std::cout << "Caught domain error: " << e.what() << std::endl; } return 0; } std::length_errorstd::length_error 派生自 std::logic_error,表示长度错误,如在容器操作中超出了最大长度限制。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::length_error("Length error occurred"); } catch (const std::length_error& e) { std::cout << "Caught length error: " << e.what() << std::endl; } return 0; } std::out_of_rangestd::out_of_range 派生自 std::logic_error,表示访问超出了有效范围。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::out_of_range("Out of range error occurred"); } catch (const std::out_of_range& e) { std::cout << "Caught out of range error: " << e.what() << std::endl; } return 0; } std::runtime_errorstd::runtime_error 派生自 std::exception,表示程序运行时错误。它是一个抽象基类,通常由派生类来具体化。 实例
#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; } std::range_errorstd::range_error 派生自 std::runtime_error,表示计算结果超出了表示范围。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::range_error("Range error occurred"); } catch (const std::range_error& e) { std::cout << "Caught range error: " << e.what() << std::endl; } return 0; } std::overflow_errorstd::overflow_error 派生自 std::runtime_error,表示算术溢出错误。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::overflow_error("Overflow error occurred"); } catch (const std::overflow_error& e) { std::cout << "Caught overflow error: " << e.what() << std::endl; } return 0; } std::underflow_errorstd::underflow_error 派生自 std::runtime_error,表示算术下溢错误。 实例
#include <iostream> #include <stdexcept> int main() { try { throw std::underflow_error("Underflow error occurred"); } catch (const std::underflow_error& e) { std::cout << "Caught underflow error: " << e.what() << std::endl; } return 0; } 使用自定义异常类除了标准异常类,我们还可以定义自己的异常类,继承自标准异常类。 实例
#include <iostream> #include <stdexcept> class MyException : public std::runtime_error { public: MyException(const std::string& message) : std::runtime_error(message) {} }; int main() { try { throw MyException("My custom exception occurred"); } catch (const MyException& e) { std::cout << "Caught custom exception: " << e.what() << std::endl; } return 0; }
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |