异常类的构建——5个子类构建
1.Exception.h 中增加ArithmetricException类
class ArithmetricException:public Exception
{
public:
ArithmetricException():Exception(0) {} //我认为这个实现没有必要
ArithmetricException(const char* message):Exception(message) {}
ArithmetricException(const char* file, int line):Exception(file, line) {}
ArithmetricException(const char* message, const char* file, int line):Exception(message,file,line) {}
ArithmetricException(const ArithmetricException& e):Exception(e) {}
ArithmetricException& operator = (const ArithmetricException& e)
{
Exception::operator =(e);
return *this;
}
};
2.Exception.h 中增加NullPointerException类
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) {}
NullPointerException& operator = (const NullPointerException& e)
{
Exception::operator =(e);
return *this;
}
};
3.Exception.h 中增加IndexOutOfBoundsException类
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;
}
};
4.Exception.h 中增加NoEnoughMemoryException类
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;
}
};
5.Exception.h 中增加InvalidParameterException类
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;
}
};
main.cpp
#include <iostream>
#include "Exception.h"
using namespace std;
using namespace DTLib;
int main()
{
try
{
THROW_EXCEPTION(ArithmetricException,"test");
}
catch(const ArithmetricException& e)
{
cout << " catch(const ArithmetricException& e)" << endl;
cout << e.message() << endl;
cout << e.location() << endl;
}
catch(const Exception& e)
{
cout << " catch(const Exception& e)" << endl;
cout << e.message() << endl;
cout << e.location() << endl;
}
return 0;
}
设计原则:
在可复用代码库设计时,尽量使用面向对象技术进行架构,尽量使用异常处理机制分离正常逻辑和异常逻辑