PPT:


代码:

#ifndef EXCEPTION_H#define EXCEPTION_Hnamespace DJLib{#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 NullPointerException : public Exception//空指针异常 NullPointerException{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){}NullPointerException& operator = (const NullPointerException& e){Exception::operator =(e);return *this;}};class IndexOutOfBoundsException : public Exception//越界异常 IndexOutOfBoundsException{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//内存不足异常 NoEnoughMemoryException{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 InvalldParameterException : public Exception//参数错误异常 InvalldParameterException{public:InvalldParameterException():Exception(0){}InvalldParameterException(const char* message) :Exception(message){}InvalldParameterException(const char* file, int line):Exception(file,line){}InvalldParameterException(const char* message,const char* file,int line):Exception(message,file,line){}InvalldParameterException(const InvalldParameterException& e):Exception(e){}InvalldParameterException& operator = (const InvalldParameterException& e){Exception::operator =(e);return *this;}};}#endif // EXCEPTION_H

#include "exception.h"#include <cstring>#include <stdlib.h>using namespace std;namespace DJLib {void Exception::init(const char* message, const char* file, int line){m_message = strdup(message);//拷贝信息到堆空间,m_message指向堆空间的信息if(file != NULL){char sl[16] = {0}; //定义一个字符数组来接收转换的字符串itoa(line,sl,10);//发生异常的行号转换为字符m_location = static_cast<char *>(malloc(strlen(file)+strlen(sl)+2));//堆空间申请内存m_location = strcpy(m_location,file);m_location = strcat(m_location,":"); //拼接m_location = strcat(m_location,sl);}else{m_location = NULL;}}Exception::Exception(const char* message){init(message,NULL,0);}Exception::Exception(const char* file, int line){init(NULL,file,line);}Exception::Exception(const char* message,const char* file,int line){init(message,file,line);}Exception::Exception(const Exception& e)//拷贝构造函数,深拷贝{m_message = strdup(e.m_message);m_location = strdup(e.m_location);}Exception& Exception::operator= (const Exception& e)//赋值操作符{if(this != &e){free(m_message);free(m_location);m_message = strdup(e.m_message);m_location = strdup(e.m_location);}return *this;}const char* Exception::message() const{return m_message;}const char* Exception::location() const{return m_location;}Exception::~Exception(){free(m_message);free(m_location);}}

#include <iostream>#include "exception.h"#include "smartpointer.h"using namespace std;using namespace DJLib;int main(){try{// throw Exception("test" ,__FILE__,__LINE__);THROW_EXCEPTION(InvalldParameterException,"test");}catch(const ArithmeticException& e)//子类放上面,父类放下面{cout<<" catch(const ArithmeticException(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}catch(const NullPointerException& e)//子类放上面,父类放下面{cout<<" catch(const NullPointerException(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}catch(const IndexOutOfBoundsException& e)//子类放上面,父类放下面{cout<<" catch(const IndexOutOfBoundsException(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}catch(const NoEnoughMemoryException& e)//子类放上面,父类放下面{cout<<" catch(const NoEnoughMemoryException(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}catch(const InvalldParameterException& e)//子类放上面,父类放下面{cout<<" catch(const InvalldParameterException(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}catch(const Exception& e)//子类放上面,父类放下面{cout<<" catch(const Exception(exception& e))" << endl;cout<< e.message() << endl;cout<< e.location() << endl;}return 0;}


