c++异常类
- Exception,接口类
- ArithmeticException类, 计算异常
- NullPointerException类,空指针异常
- IndexOutOfBoundsException类,越界异常
- NoEnoughtMemoryException,内存不足异常
- InvalidParameterException,参数错误异常
#define THROW_EXCEPTION(e,m) (throw e(m,__FILE__,__LINE__))
class Exception
{
protected:
char* m_message;
char* m_location;
void init(const char* messge, const char* file, int line);
public:
Exception(const char* message);
Exception(const char* file, int line);
Exception(const char* messge, 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& 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& 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& operator =(const IndexOutOfBoundsException& e)
{
Exception::operator =(e);
return *this;
}
};
class NoEnoughtMemoryException : public Exception
{
public :
NoEnoughtMemoryException ():Exception (0) {}
NoEnoughtMemoryException (const char* message):Exception(message) {}
NoEnoughtMemoryException(const char * file,int line):Exception(file,line){}
NoEnoughtMemoryException(const char* message,const char *file, int line):Exception(message,file,line){}
NoEnoughtMemoryException& operator =(const NoEnoughtMemoryException& 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& operator =(const InvalidParameterException& 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& operator =(const InvalidParameterException& e)
{
Exception::operator =(e);
return *this;
}
};
void Exception::init(const char* messge, const char* file, int line)
{
m_message = (messge ? strdup(messge) : NULL);
if (file != NULL)
{
char sl[16] = { 0 };
snprintf(sl, sizeof(sl), "%d", line);
m_location = static_cast<char*>(malloc(strlen(file) + strlen(sl) + 2));
if (m_location != NULL)
{
strcpy(m_location,file);
strcat(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* messge, const char* file, int line)
{
init(messge, 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);
}