您好,登錄后才能下訂單哦!
C++內(nèi)置了異常處理的語法元素try.....catch.....
1.try語句處理正常代碼邏輯
2.catch語句處理異常情況
3.try語句中的異常由對應(yīng)的catch語句處理
常見的語句如下
try
{
double r=divide(1,0);
}
catch(...)
{
cout<<"the result..."<<endl;
}
C++通過throw語句拋出異常信息
double divide(double a,double b)
{
const double delta=0.00000000001;
double ret=0;
if(!((-delta<b)&&(b<delta)))
{
ret=a/b;
}
else
{
throw 0;//產(chǎn)生除0異常
}
return ret;
}
C++異常處理分析
throw拋出的異常必須被catch處理--當(dāng)前函數(shù)能夠處理異常,程序機(jī)械往下執(zhí)行,當(dāng)前函數(shù)無法處理異常,則函數(shù)停止執(zhí)行,并返回.未被處理的異常會順著函數(shù)調(diào)用棧向上傳播,直到被處理為止,否則程序?qū)⑼V箞?zhí)行
//代碼示例
#include <iostream>
using namespace std;
double divide(double a, double b)
{
const double delta = 0.000000000000001;
double ret = 0;
if( !((-delta < b) && (b < delta)) ) {
ret = a / b;
}
else {
throw 0; // 產(chǎn)生除 0 異常
}
return ret;
}
void Demo1()
{
try
{
throw 'c';
}
catch(int i)
{
cout << "catch(int i)" << endl;
}
catch(double d)
{
cout << "catch(double d)" << endl;
}
catch(char c)
{
cout << "catch(char c)" << endl;
}
}
void Demo2()
{
throw 0.0001; // "D.T.Software"; // const char*
}
int main()
{
cout << "main() begin" << endl;
try
{
double c = divide(1, 1);
cout << "c = " << c << endl;
}
catch(...)
{
cout << "Divided by zero..." << endl;
}
Demo1();
try
{
Demo2();
}
catch(char* c)
{
cout << "catch(char* c)" << endl;
}
catch(const char* cc)
{
cout << "catch(char* cc)" << endl;
}
catch(...)
{
cout << "catch(...)" << endl;
}
cout << "main() end" << endl;
return 0;
}
異常的類型可以是自定義類類型,對于類類型異常的匹配依舊是至上而下嚴(yán)格匹配,賦值兼容性原則在異常匹配中依然適用
異常類的功能定義
異常類 | 功能定義 |
---|---|
ArithmeticException | 計量異常 |
NullPointerException | 空指針異常 |
IndexOutBoundsException | 越界異常 |
NoEnoughMemoryException | 內(nèi)存不足異常 |
InvaildParameterException | 參數(shù)異常 |
#ifndef EXCEPTION_H
#define EXCEPTION_H
/*
異常的類型可以是自定義類類型
對于類類型異常的匹配依舊是至上而下嚴(yán)格匹配
一般而言
匹配子類異常的catch放在上部
匹配父類異常的catch放在下部
*/
#include "Object.h"
namespace MyLib
{
//定義宏THROW_EXCEPTION,拋出異常時直接寫異常類型及異常信息即可
#define THROW_EXCEPTION(e, m) (throw e(m, __FILE__, __LINE__))
class Exception:public Object
{
protected:
char* m_message;//異常的信息
char* m_location;//異常的位置
void init(const char* message,const char *file,int line);
public:
Exception(const char* message);
Exception(const char *file,int line);
Exception(const char* message,const char *file,int line);
Exception(const Exception& e);
Exception& operator=(const Exception& e);
virtual const char* message()const;
virtual const char* location()const;
virtual ~Exception();//純虛函數(shù)
};
class ArithmeticException : public Exception
{
public:
ArithmeticException():Exception(0){}
ArithmeticException(const char* message):Exception(message){}
ArithmeticException(const char* file,int line):Exception(file,line){}
ArithmeticException(const char*message,const char*file,int line):Exception(message,file,line){}
ArithmeticException(const ArithmeticException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
ArithmeticException&operator =(const ArithmeticException& e)
{
Exception::operator =(e);
return *this;
}
};
class NullPointerException : public Exception
{
public:
NullPointerException():Exception(0){}
NullPointerException(const char* message):Exception(message){}
NullPointerException(const char* file,int line):Exception(file,line){}
NullPointerException(const char*message,const char*file,int line):Exception(message,file,line){}
NullPointerException(const NullPointerException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
NullPointerException&operator =(const NullPointerException& e)
{
Exception::operator =(e);
return *this;
}
};
class indexOutOfBoundsException : public Exception
{
public:
indexOutOfBoundsException():Exception(0){}
indexOutOfBoundsException(const char* message):Exception(message){}
indexOutOfBoundsException(const char* file,int line):Exception(file,line){}
indexOutOfBoundsException(const char*message,const char*file,int line):Exception(message,file,line){}
indexOutOfBoundsException(const indexOutOfBoundsException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
indexOutOfBoundsException&operator =(const indexOutOfBoundsException& e)
{
Exception::operator =(e);
return *this;
}
};
class NoEoughMemoryException : public Exception
{
public:
NoEoughMemoryException():Exception(0){}
NoEoughMemoryException(const char* message):Exception(message){}
NoEoughMemoryException(const char* file,int line):Exception(file,line){}
NoEoughMemoryException(const char*message,const char*file,int line):Exception(message,file,line){}
NoEoughMemoryException(const NoEoughMemoryException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
NoEoughMemoryException&operator =(const NoEoughMemoryException& e)
{
Exception::operator =(e);
return *this;
}
};
class InvalidOperationException : public Exception
{
public:
InvalidOperationException():Exception(0){}
InvalidOperationException(const char* message):Exception(message){}
InvalidOperationException(const char* file,int line):Exception(file,line){}
InvalidOperationException(const char*message,const char*file,int line):Exception(message,file,line){}
InvalidOperationException(const InvalidOperationException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
InvalidOperationException&operator =(const InvalidOperationException& e)
{
Exception::operator =(e);
return *this;
}
};
class InvalidParameterException : public Exception
{
public:
InvalidParameterException():Exception(0){}
InvalidParameterException(const char* message):Exception(message){}
InvalidParameterException(const char* file,int line):Exception(file,line){}
InvalidParameterException(const char*message,const char*file,int line):Exception(message,file,line){}
InvalidParameterException(const InvalidParameterException& e):Exception(e){}//拷貝構(gòu)造函數(shù)
InvalidParameterException&operator =(const InvalidParameterException& e)
{
Exception::operator =(e);
return *this;
}
};
}
#endif // EXCEPTION_H
三 頂層父類的創(chuàng)建
創(chuàng)建頂層父類的意義--遵循經(jīng)典設(shè)計準(zhǔn)則,所有的數(shù)據(jù)都繼承Object類,定義動態(tài)內(nèi)存申請的行為,提高代碼的移植性
頂層父類的接口定義
class Object
{
public:
void* operator new(unsigned int size)throw();
void operator delete(void* p);
void* operator new[](unsigned int size)throw();
void operator delete[](void* p);
~Object();
};
/*
Object類是所寫的頂層父類
Object類用于統(tǒng)一內(nèi)存申請的行為
在堆中創(chuàng)建Object子類的對象,失敗時返回NULL值
Object類為純虛父類,所有子類都能進(jìn)行動態(tài)類型識別
*/
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。