<span style="font-size:18px;">throw是C++中關鍵字,用來拋出異常。不使用關鍵字throw,try就什麼異常也捕獲不了,throw的使用語法規則:throw data; </span>
<span style="font-size:18px;"> data爲定義的異常數據類型.throw拋出異常通常用來解決,內存空間不足,數組下標,數據類型初始化數值類型不對。</span>
<span style="font-size:18px;">例如char *q=0; throw 0;
數組爲10個,輸入數據爲11個。</span>
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int n,string nam)
{
cout<<"constructor-"<<n<<endl;
num=n;name=nam;
}
~Student()
{
cout<<"destructor-"<<num<<endl;
}
void get_data();
private:
int num;
string name;
};
void Student::get_data()
{
if(num==0) throw num;
else cout<<num<<" "<<name<<endl;
cout<<"in get_data()"<<endl;
}
void fun()
{
Student stud1(1101,"tan");
stud1.get_data();
try
{
Student stud2(0,"Li");
stud2.get_data();
}
catch
(int n)
{cout<<"num="<<n<<",error!"<<endl;}
}
int main()
{
cout<<"main begin"<<endl;
cout<<"call fun()"<<endl;
fun();
cout<<"main end"<<endl;
return 0;
}
<span style="font-size:18px;">在上面程序中,第一個函數fun中,先執行了stud1的構造函數....</span>
<span style="font-size:18px;">而在執行stud2時,構造完之後,num=0,拋出異常在catch中解決掉解決辦法在這個函數中是delete掉這個stud2的初始化。</span>
<span style="font-size:18px;">運行結果如圖所示.</span>
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><img src="https://img-blog.csdn.net/20160908170926035" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;" alt="" /></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">對於抽象數據類型的異常對象。catch(…)同樣有效,例程如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">class MyException
{
public:
protected:
int code;
};</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 準備拋出一個異常." << endl;
//這裏拋出一個異常(其中異常對象的數據類型是MyException)
throw MyException();
}
//catch(MyException& value )
//注意這裏catch語句
catch( …)
{
cout << "在catch(…) block中, MyException類型的異常對象被處理" << endl;
}
}</p>
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">一.int類型的異常被catch(…)抓獲了,再來另一個例子:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 準備拋出一個異常." << endl;
//這裏拋出一個異常(其中異常對象的數據類型是double,值爲0.5)
throw 0.5;
}
//catch( double& value )
//注意這裏catch語句
catch( …)
{
cout << "在 catch(…) block 中, double類型的異常對象也被處理" << endl;
}
}</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">二.對於抽象數據類型的異常對象。catch(…)同樣有效,例程如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">class MyException
{
public:
protected:
int code;
};</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 準備拋出一個異常." << endl;
//這裏拋出一個異常(其中異常對象的數據類型是MyException)
throw MyException();
}
//catch(MyException& value )
//注意這裏catch語句
catch( …)
{
cout << "在catch(…) block中, MyException類型的異常對象被處理" << endl;
}
}</p>