日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++异常处理分析

發布時間:2023/12/4 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++异常处理分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++異常處理基本語法:
代碼如下:

#include <iostream> using namespace std;int divide(int x, int y) {if (y == 0) throw y;return x / y; }void test01() {//試著去捕獲異常try{divide(10, 0);}/*catch (int){cout << "除數為0!" << endl;} */catch (int e){cout << "除數為" << e << "!" << endl;} }int main() {test01();return 0; }
  • C++異常機制跨函數
  • C++異常必須處理
  • 代碼如下:

    #include <iostream> using namespace std;int divide(int x, int y) {if (y == 0) throw y;return x / y; }void test01() {//試著去捕獲異常try{divide(10, 0);}/*catch (int){cout << "除數為0!" << endl;} */catch (int e){cout << "除數為" << e << "!" << endl;} }void callDivide(int x, int y) {divide(x, y); }void test02() {try {callDivide(10,0);}catch (int e){cout << "除數為" << e << "!" << endl;} }int main() {/*test01();*/test02();return 0; }

    棧解旋
    異常被拋出后,從進入try塊起,這期間在棧上構造的所有對象,都會被自動析構。析構的順序與構造的順序相反,這一過程稱為棧的解旋。

    代碼如下:

    #include <iostream> using namespace std;class Person { public:Person(){cout << "對象構建" << endl;}~Person() {cout << "對象析構" << endl;} };int divide(int x, int y) {Person p1, p2;if (y == 0) throw y;return x / y; }void test01() {//試著去捕獲異常try{divide(10, 0);}/*catch (int){cout << "除數為0!" << endl;} */catch (int e){cout << "異常捕獲"<< endl;} }void callDivide(int x, int y) {divide(x, y); }int main() {test01();return 0; }

    測試結果:

    異常接口聲明:


    由于C++編譯器,忽略C++異常規范,所有在VS可以編譯通過運行,但是在linux是不行的。

    代碼如下:

    #include <iostream>using namespace std;//這個函數只能拋出int,float,char三種類型異常,拋出其他的就報錯 void func() throw(int ,float,char) {throw "abc"; }//不能拋出任何異常 void func02() throw() {throw - 1; }//可以拋出任何異常 void func03() {throw - 1; }int main() {try {func();}catch (char * str){cout << str << endl;}catch (int e){cout << "異常" << endl;}catch (...){cout << "未知異常" << endl;}return 0; }

    C標準異常類使用舉例和編寫自己的異常類:

    代碼如下:

    #include <iostream> #include <stdexcept> using namespace std;class Person { public:Person(){mAge = 0;}void setAge(int age){if (age < 0 || age > 100){throw out_of_range("年齡應該在0-100之間!");}this->mAge = age;}public:int mAge; };void test01() {Person p;try {p.setAge(1000);}/*catch (out_of_range e){cout << e.what() << endl;}*/catch (exception e){cout << e.what() << endl;}}class MyOutOfRange :public exception { public:MyOutOfRange(const char *error){pError = new char[strlen(error) + 1];strcpy(pError, error);}~MyOutOfRange(){if (pError != nullptr){delete[] pError;}}virtual const char *what() const{return pError;}public:char *pError; };void fun02() {throw MyOutOfRange("我自己的out_of_range!");}void test02() {try{fun02();}catch (exception &e){cout << e.what() << endl;} }int main() {/*test01();*/test02();return 0; }

    繼承在異常中的應用:
    代碼如下:

    #include <iostream> using namespace std;//異常基類 class BaseMyException { public:virtual void what() = 0;virtual ~BaseMyException(){} };class TargetSpaceNullException :public BaseMyException { public:virtual void what(){cout << "目標空間空!" << endl;}~TargetSpaceNullException() {} };class SourceSpaceNullException :public BaseMyException { public:virtual void what(){cout << "原空間為空!" << endl;}~SourceSpaceNullException(){} };void copy_str(char *taget, const char *source) {if (taget == nullptr){throw TargetSpaceNullException();}if (source == nullptr){throw SourceSpaceNullException();}int len = strlen(source) + 1;while (*source != '\0'){*taget = *source;taget++;source++;} }int main() {const char *source = "abcdefg";char buf[1024] = { 0 };try {copy_str(buf, source);}catch (BaseMyException &ex){ex.what();}cout << buf << endl;return 0; }

    總結

    以上是生活随笔為你收集整理的C++异常处理分析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。