1.C++异常处理
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 <<"除法異常已經處理";
???????return -1;
???}
}
?
intintdivA(inta,intb)
{
???returna / b;
}
?
void?main()
{
???intx,y;
???cin >>x >>y;
???try
???{
???????if (y == 0)
???????{
???????????throw "被除數為0";
???????}
???????elseif (x == 0)
???????{
???????????throw "除數為0";
???????}
???}
???//說明捕獲的可以使字符串
???catch (constchar *s)
???{
???????if (strcmp(s,"被除數為0") == 0)
???????{
???????????cout <<"被除數為0異常,請重新輸入";
???????????cin >>x >>y;
???????}
???????elseif (strcmp(s,"除數為0")==0)
???????{
???????????cout <<"除數為0異常,請重新輸入";
???????????cin >>x >>y;
???????}
???}
???std::cout << intdiv(x,y);
???cin.get();
???cin.get();
???cin.get();
}
運行結果是:
2.異常處理高級
案例:
#include<iostream>
#include<string>
?
usingnamespacestd;
?
classbox?//正方體
{
public:
???box(intdata)
???{
???????std::cout << "開始構造" << 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();
}
當代碼如下時,運行結果不一樣:
//boxnewbox(0);
//boxnewbox(99);
???????
//boxnewbox(100);
//std::cout<< newbox.gettiji() << endl;
?
boxnewbox(100000);
當前運行結果:
3內存分配異常的處理(std::nothrow)
#include<iostream>
#include<new>
usingnamespacestd;
?
struct?big
{
???doubledb[200000];
};
?
//測試使用異常使得內存異常的時候仍然能夠正常使用
voidmain()
{
???big *p1, *p2;
???//使用std::nothrow讓異常不拋出
???p1 =new(std::nothrow)big[1000];
?
???p2 =new(std::nothrow)big[1000];
?
???if (p1 ==NULL ||p2 ==NULL)
???{
???????cout <<"內存分配失敗";
???}
???cin.get();
}
4.內存異常
案例:
#include<iostream>
#include<string>
?
usingnamespacestd;
?
classstudent
{
public:
???student()
???{
???????cout <<"構造" << endl;
???}
???~student()
???{
???????cout <<"析構" << endl;
???}
};
?
classX
{
public:
???void *p;//存儲地址
???charstr[30];
???X(void *pt) :p(pt)//這時候pt傳遞進來的是內存地址
???{
???}
};//處理異常
?
boolquit =false;
?
voidrun()
{
???student *p =newstudent;
???//delete p;
???//p = nullptr;
?
???//異常檢測泄露
???if (p!=nullptr)
???{
???????quit =true;
???????if (quit ==true)
???????{
???????????throwX(reinterpret_cast<void *>(p));//拋出異常,調用構造函數
???????}
???}
}
?
voidmain()
{
???try
???{
???????run();
???}
???catch (Xe)
???{
???????cout <<"內存異常,內存泄露" << e.p;
???}
?
???cin.get();
}
5.異常的虛函數
#include<iostream>
#include<string>
usingnamespacestd;
?
classbox??//正方體
{
public:
???box(intdata)
???{
???????cout <<"開始構造";
???????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()//虛函數
???????{
???????????cout <<"wrong" <<endl;
???????}
???};
???//注意:這里繼承了wrong
???classbig :publicwrong
???{
???public:
???????//實現了虛函數
???????voidshow()
???????{
???????????cout <<"big?wrong" <<endl;
???????}
???};
???//注意:這里繼承了wrong
???classsmall :publicwrong
???{
???public:
???????//實現了虛函數
???????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";
???????}
???}
???//虛函數一個接口處理多個錯誤
???//引用是指針實現的,用一個父類的引用
???catch (box::wrong &wrong1)
???{
???????wrong1.show();
???}
?
???cin.get();
}
運行結果:
6.模板類的異常
#include<iostream>
usingnamespacestd;
?
//typename會明確類型
?
//模板的異常,處理通用的數據類型,類中包含一個如果使用
//虛函數,虛函數可以指針,引用來實現
//異常處理機制,一個接口處理通用的異常
?
template<classT>
classArray
{
public:
???classwrong
???{
???public:
???????//虛函數
???????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();
}
運行結果:
?
?
總結
- 上一篇: 24合成模式
- 下一篇: 歼16战斗机和重型战斗机相比性能的差别?