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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

1.C++异常处理

發(fā)布時間:2024/9/27 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1.C++异常处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


1異常捕獲案例1

#include<iostream>

#include<string>

?

usingnamespacestd;

?

//標識錯誤的類型

classwrong{};

?

intintdiv(inta,intb)

{

???try

???{

???????if (b == 0)

???????{

???????????throw 10;//可以是任何對象?wrong()

???????}

???????intc = a /b;

???????returnc;

???}

???catch (intdata)

???{

???????cout <<"除法異常已經(jīng)處理";

???????return -1;

???}

}

?

intintdivA(inta,intb)

{

???returna / b;

}

?

void?main()

{

???intx,y;

???cin >>x >>y;

???try

???{

???????if (y == 0)

???????{

???????????throw "被除數(shù)為0";

???????}

???????elseif (x == 0)

???????{

???????????throw "除數(shù)為0";

???????}

???}

???//說明捕獲的可以使字符串

???catch (constchar *s)

???{

???????if (strcmp(s,"被除數(shù)為0") == 0)

???????{

???????????cout <<"被除數(shù)為0異常,請重新輸入";

???????????cin >>x >>y;

???????}

???????elseif (strcmp(s,"除數(shù)為0")==0)

???????{

???????????cout <<"除數(shù)為0異常,請重新輸入";

???????????cin >>x >>y;

???????}

???}

???std::cout << intdiv(x,y);

???cin.get();

???cin.get();

???cin.get();

}

運行結(jié)果是:

2.異常處理高級

案例:

#include<iostream>

#include<string>

?

usingnamespacestd;

?

classbox?//正方體

{

public:

???box(intdata)

???{

???????std::cout << "開始構(gòu)造" << std::endl;

???????if (data == 0)

???????{??

???????????//使用zero拋出異常

???????????zeroz1;

???????????z1.errorcode = 22;

???????????throwz1;

???????}

???????elseif (data > 0 &&data < 100)

???????{

???????????//注意下面是一個(),并非{}

???????????throw?small();

???????}

???????elseif (data>=100 &&data<=10000)

???????{

???????????a =data;

???????}

???????else

if (data>10000)

???????{

???????????//使用big拋出異常

???????????throwbig();

???????}

???????else

???????{

???????????//使用wrong拋出異常

???????????throwwrong();

???????}

???}

?

???//用于獲得體積

???intgettiji()

???{

???????returna *a *a;

???}

???classzero{

???public:

???????interrorcode;

???};

???classwrong{};

???classbig{};

???classsmall{};

private:

???inta;//變長

};

?

voidmain()

{

???try

???{

???????//box newbox(0);

???????//box newbox(99);

???????

???????//box newbox(100);

???????//std::cout <<newbox.gettiji() << endl;

?

???????boxnewbox(100000);

???}

???catch (box::zerow)

???{

???????if (w.errorcode == 22)

???????{

???????????cout <<"大小:錯誤編碼:22:正方體長度不可以為0";

???????}

???????else

???????{

???????????cout <<"錯誤編碼:非22號,錯誤:正方體長度不可以為0";

???????}

???}

???catch (box::wrong)

???{

???????cout <<"正方體長度異常";

???}

???catch (box::big)

???{

???????cout <<"正方體長度太長";

???}

???catch (box::small)

???{

???????cout <<"正方體長度太短";

???}

???cin.get();

}

當代碼如下時,運行結(jié)果不一樣:

//boxnewbox(0);

//boxnewbox(99);

???????

//boxnewbox(100);

//std::cout<< newbox.gettiji() << endl;

?

boxnewbox(100000);

當前運行結(jié)果:

3內(nèi)存分配異常的處理(std::nothrow

#include<iostream>

#include<new>

usingnamespacestd;

?

struct?big

{

???doubledb[200000];

};

?

//測試使用異常使得內(nèi)存異常的時候仍然能夠正常使用

voidmain()

{

???big *p1, *p2;

???//使用std::nothrow讓異常不拋出

???p1 =new(std::nothrow)big[1000];

?

???p2 =new(std::nothrow)big[1000];

?

???if (p1 ==NULL ||p2 ==NULL)

???{

???????cout <<"內(nèi)存分配失敗";

???}

???cin.get();

}

4.內(nèi)存異常

案例:

#include<iostream>

#include<string>

?

usingnamespacestd;

?

classstudent

{

public:

???student()

???{

???????cout <<"構(gòu)造" << endl;

???}

???~student()

???{

???????cout <<"析構(gòu)" << endl;

???}

};

?

classX

{

public:

???void *p;//存儲地址

???charstr[30];

???X(void *pt) :p(pt)//這時候pt傳遞進來的是內(nèi)存地址

???{

???}

};//處理異常

?

boolquit =false;

?

voidrun()

{

???student *p =newstudent;

???//delete p;

???//p = nullptr;

?

???//異常檢測泄露

???if (p!=nullptr)

???{

???????quit =true;

???????if (quit ==true)

???????{

???????????throwX(reinterpret_cast<void *>(p));//拋出異常,調(diào)用構(gòu)造函數(shù)

???????}

???}

}

?

voidmain()

{

???try

???{

???????run();

???}

???catch (Xe)

???{

???????cout <<"內(nèi)存異常,內(nèi)存泄露" << e.p;

???}

?

???cin.get();

}

5.異常的虛函數(shù)

#include<iostream>

#include<string>

usingnamespacestd;

?

classbox??//正方體

{

public:

???box(intdata)

???{

???????cout <<"開始構(gòu)造";

???????if (data == 0)

???????{

???????????zeroz1(22);

???????????z1.seterror(21);

???????????throwz1;

???????}

???????elseif (data > 0 &&data<100)

???????{

???????????throwsmall();

???????}

???????else?if (data>10000)

???????{

???????????throwbig();

???????}

???????elseif (data > 100 &&data < 10000)

???????{

???????????a = data;

???????}

???????else

???????{

???????????throwwrong{};

???????}

???}

?

???int?gettiji()

???{

???????return?a*a*a;

???}

?

???classwrong

???{

???public:

???????virtualvoidshow()//虛函數(shù)

???????{

???????????cout <<"wrong" <<endl;

???????}

???};

???//注意:這里繼承了wrong

???classbig :publicwrong

???{

???public:

???????//實現(xiàn)了虛函數(shù)

???????voidshow()

???????{

???????????cout <<"big?wrong" <<endl;

???????}

???};

???//注意:這里繼承了wrong

???classsmall :publicwrong

???{

???public:

???????//實現(xiàn)了虛函數(shù)

???????voidshow()

???????{

???????????cout <<"small?wrong" <<endl;

???????}

???};

?

???classzero :publicsmall?//兩種錯誤的處理方式都接受

???{

???public:

???????interrorcode;

???????zero(inti) :errorcode(i)

???????{

???????}

???????void?seterror(inti)

???????{

???????????errorcode =i;

???????}

???};

private:

???inta;//變長

};

?

voidmain()

{

???try

???{

???????boxnewbox(11168);

???}

???catch (box::zerow)

???{

???????if (w.errorcode == 22)

???????{

???????????cout <<"22號錯誤正方體長度不可以為0";

???????}

???????else

???????{

???????????cout <<"22號錯誤正方體長度不可以為0";

???????}

???}

???//虛函數(shù)一個接口處理多個錯誤

???//引用是指針實現(xiàn)的,用一個父類的引用

???catch (box::wrong &wrong1)

???{

???????wrong1.show();

???}

?

???cin.get();

}

運行結(jié)果:

6.模板類的異常

#include<iostream>

usingnamespacestd;

?

//typename會明確類型

?

//模板的異常,處理通用的數(shù)據(jù)類型,類中包含一個如果使用

//虛函數(shù),虛函數(shù)可以指針,引用來實現(xiàn)

//異常處理機制,一個接口處理通用的異常

?

template<classT>

classArray

{

public:

???classwrong

???{

???public:

???????//虛函數(shù)

???????virtualvoidshow()

???????{

???????????cout <<" wrong " <<typeid(T).name();

???????}

???};

???classbig :publicwrong

???{

???public:

??????intx;

???????big(intn) :x(n){}

???????voidshow()

???????{

???????????cout <<"big wrong"<<x <<"?" <<typeid(T).name();

???????}

???};

???classsmall :publicwrong

???{

???public:

???????intx;

???????small(intn) :x(n){}

???????voidshow()

???????{

???????????cout <<"small?wrong" <<x <<"?" <<typeid(T).name();;

???????}

???};

???Array(intn)

???{

???????if (n > 0 &&n<10)

???????{

???????????throwsmall(n);

???????}

???????elseif (n>10000)

???????{

???????????throwbig(n);

???????}

???????elseif (n < 0)

???????{

???????????throwwrong();

???????}

???????else

???????{

???????????p =newT[n];

???????????size = n;

???????}

???}

private:

???intsize;

???T *p;

};

?

voidmain()

{

???try

???{

???????Array<double>may(1);

???}

???catch (Array<double>::wrong & wrong1)

???{

???????wrong1.show();

???}

???cin.get();

}

運行結(jié)果:

?

?

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。