-
异常的基本处理
-
异常处理机制:暂缓问题处理,不在当前函数中处理,在他调用者中处理
-
什么是异常,任何东西都可以认为是异常,错误只是异常的一种
-
异常一旦被抛出,不做处理,如果引发异常,会调用默认abort终止程序
-
捕获和处理异常
-
throw 抛出异常,(可以理解为返回值,值是任何类型都可以,使我们处理异常一个参照)
-
try(检查,捕获)和catch(处理异常)
-
-
不存在异常的描述
-
throw ()
-
noexcept
-
-
#include<iostream> using namespace std; int Division(int a, int b) { if (0==b) //什么异常 { //throw 1; //怎么抛出 //string str = "123"; //throw str; throw 1.2; } return a / b; } //catch和if else_if 执行机制是一样的,只能执行一个匹配项 //删减符 ... 任何类型异常都捕获 //catch(...) void print() throw() { cout << "当前函数不存在抛出异常操作" << endl; } void printData() noexcept { cout << "新的描述:不存在抛出异常" << endl; //throw 0; 编译会出错,不能抛出 } int main() { try { Division(2, 0);//引发异常不作处理 cout << "上面如果出错,就不会执行这行代码" << endl; } catch (int) { cout << "捕获抛出类型为int的异常" << endl; cout << "除数不能为零" << endl; } catch (string) { cout << "捕获抛出类型为string的异常" << endl; cout << "除数不能为零" << endl; } catch (...) { cout << "捕获任何类型的异常" << endl; cout << "除数不能为零" << endl; } return 0; }
-
-
异常处理中的传参
-
catch(int a) //隐藏一个传参操作
-
想要处理抛出字符串的异常处理,注意一下string类型与const char* 类型的区别
-
也可以抛出自己类的对象
-
#include<iostream> #include <string> using namespace std; class Error { public: Error(const char* str = "未知错误") :str(str) {} const char* what()const { return str.c_str(); } protected: string str; private: }; int Division(int a, int b) { if (0 == b) { //注意string和char*的区别 //throw string("除数不能为零"); //throw "除数不能为零"; throw Error("除数不能为零"); } return a / b; } void insertArray(int array[], int& index, int posData, int Length) { if (index >=Length) { throw Error("数组下标溢出"); } array[index] = posData; index++; } int main() { try { Division(2, 0); } catch (string str){ cout << str << endl; } catch (const char* str) { cout << str << endl; } catch (Error str) { cout << str.what() << endl; } try { int array[3]; int index = 0; for (int i = 0; i < 4; i++) { insertArray(array, index, i, 3); } for (int i=0; i < 3; i++) { cout << array[i] << " "; } } catch (Error str) { cout << str.what() << endl; } return 0; }
-
-
自定义异常处理
-
写一个类描述异常
-
继承标准库中的类描述异常
-
#include<iostream> #include<string> #include <cstring> using namespace std; class myException:public exception { public: myException(string str):exception(str.c_str()){} protected: private: }; void insertArray(int array[], int& index, int posData, int Length) { if (index >= Length) { throw myException("数组下标溢出"); } array[index] = posData; index++; } void deleteArray(int array[], int& index) { if (0 >= index) { throw myException("数组为空,无法删除"); } index--; } int main() { try { int array[3]; int index = 0; int length = 3; for (int i = 0; i < 3; i++) { insertArray(array, index, i, length); } for (int i = 0; i < 4; i++) { deleteArray(array, index); } for (int i = 0; i < index; i++) { cout << array[i] << " "; } } catch (myException& object) { cout << object.what() << endl; } return 0; }
-
-
标准库中的异常
#include <exception> #include <iostream> using namespace std; class Exception { public: Exception(const char* ptr = "UNKNOW") :ptr(const_cast<char*>(ptr)) {} virtual const char* what() const { return ptr; } protected: char* ptr; }; class Bad_alloc :public Exception { public: Bad_alloc(const char* _Message = "bad exception") :Exception(_Message) {} protected: }; class Run_time :public Exception { public: Run_time(const char* _Message = "run_time error") :Exception(_Message) {} protected: }; int main() { try { while (1) { int* p = new int[1024 * 1024 * 10]; } } catch (bad_alloc& object) { cout << object.what() << endl; } return 0; }
11.C++异常处理
最新推荐文章于 2024-01-14 21:48:50 发布
本文详细讲解了异常处理的基础概念,包括异常的抛出、捕获和处理,以及如何使用`try-catch`结构、自定义异常类和标准库异常。通过实例演示了如何在C++中实现不同类型的异常处理,以及如何传递参数和处理不同类型的异常对象。
摘要由CSDN通过智能技术生成