Poco::Exception:
Poco::Exception 是所有Poco异常的父类,Poco::Exception由std::exception派生
#include "Poco/Exception.h"
自定义 Exceptions:
抛出捕获异常:
poco提供2个宏完成自定义异常声明和初始化工作
POCO_DECLARE_EXCEPTION
POCO_IMPLEMENT_EXCEPTION
-
#define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \
-
POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, 0)
-
#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
-
CLS::CLS(int code): BASE(code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, int code): BASE(msg, code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code) \
-
{ \
-
} \
-
CLS::CLS(const CLS& exc): BASE(exc) \
-
{ \
-
} \
-
CLS::~CLS() throw() \
-
{ \
-
} \
-
CLS& CLS::operator = (const CLS& exc) \
-
{ \
-
BASE::operator = (exc); \
-
return *this; \
-
} \
-
const char* CLS::name() const throw() \
-
{ \
-
return NAME; \
-
} \
-
const char* CLS::className() const throw() \
-
{ \
-
return typeid(*this).name(); \
-
} \
-
Poco::Exception* CLS::clone() const \
-
{ \
-
return new CLS(*this); \
-
} \
-
void CLS::rethrow() const \
-
{ \
-
throw *this; \
-
}
-
#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
-
CLS::CLS(int code): BASE(code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, int code): BASE(msg, code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code) \
-
{ \
-
} \
-
CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code) \
-
{ \
-
} \
-
CLS::CLS(const CLS& exc): BASE(exc) \
-
{ \
-
} \
-
CLS::~CLS() throw() \
-
{ \
-
} \
-
CLS& CLS::operator = (const CLS& exc) \
-
{ \
-
BASE::operator = (exc); \
-
return *this; \
-
} \
-
const char* CLS::name() const throw() \
-
{ \
-
return NAME; \
-
} \
-
const char* CLS::className() const throw() \
-
{ \
-
return typeid(*this).name(); \
-
} \
-
Poco::Exception* CLS::clone() const \
-
{ \
-
return new CLS(*this); \
-
} \
-
void CLS::rethrow() const \
-
{ \
-
throw *this; \
-
}
一个简单例子:
-
#include "Poco/Exception.h"
-
#include <typeinfo>
-
#include <stdio.h>
-
-
#define MyExceptionAPI
-
-
POCO_DECLARE_EXCEPTION(MyExceptionAPI, MyException, Poco::Exception)
-
-
POCO_IMPLEMENT_EXCEPTION(MyException, Poco::Exception, "MyException")
-
-
-
-
int throwExceptionFun()
-
{
-
throw MyException("throwException");
-
-
return 0;
-
}
-
-
int main()
-
{
-
-
try
-
{
-
throwExceptionFun();
-
}
-
catch (MyException &e)
-
{
-
printf("catch exception:%s\n", e.message().c_str());
-
//message从Poco::Exception 继承,输出异常消息字符串"throwException"
-
}
-
return 0;
-
}
Debugging Utilities:
阅读(3349) | 评论(0) | 转发(0) |