c++异常类的构建

    技术2023-08-31  90

    两段代码:

    #ifndef EXCEPTION_H #define EXCEPTION_H namespace DTLib { #define THROW_EXCEPTION(e,m)(throw e(m,__FILE__,__LINE__)) class Exception { 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()=0; }; 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){} ArithmeticException& operator =(const ArithmeticException& e){ Exception::operator =(e); return *this; } }; class NullPointerExcepyion :public Exception { public: NullPointerExcepyion():Exception(0){} NullPointerExcepyion(const char* message) : Exception(message){} NullPointerExcepyion(const char* file,int line) : Exception(file,line){} NullPointerExcepyion(const char* message,const char* file,int line) : Exception(message,file,line){} NullPointerExcepyion(const NullPointerExcepyion& e):Exception(e){} NullPointerExcepyion& operator =(const NullPointerExcepyion& 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){} IndexOutOfBoundsException& operator =(const IndexOutOfBoundsException& e){ Exception::operator =(e); return *this; } }; class NoEnoughMemoryException :public Exception { public: NoEnoughMemoryException():Exception(0){} NoEnoughMemoryException(const char* message) : Exception(message){} NoEnoughMemoryException(const char* file,int line) : Exception(file,line){} NoEnoughMemoryException(const char* message,const char* file,int line) : Exception(message,file,line){} NoEnoughMemoryException(const NoEnoughMemoryException& e):Exception(e){} NoEnoughMemoryException& operator =(const NoEnoughMemoryException& 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){} InvalidParameterException& operator =(const InvalidParameterException& e){ Exception::operator =(e); return *this; } }; } #endif // EXCEPTION_H #include <iostream> #include"exception.h" using namespace std; using namespace DTLib; int main() { const char*message="test"; try{ //throw Exception(message,__FILE__,__LINE__); THROW_EXCEPTION(ArithmeticException,message); } catch(const Exception& e) //为什么抛出的ArithmeticException 却可以捕获Exception?赋值兼容 { cout<<"1"<<endl; cout<<e.message()<<endl; cout<<e.location()<<endl; } catch(const ArithmeticException& e) //说明了异常捕获是有顺序的 { cout<<"2"<<endl; cout<<e.message()<<endl; cout<<e.location()<<endl; } return 0; }

     

    Processed: 0.008, SQL: 9