Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185599
  • 博文数量: 60
  • 博客积分: 1597
  • 博客等级: 上尉
  • 技术积分: 461
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-20 13:24
文章分类

全部博文(60)

文章存档

2017年(15)

2016年(6)

2015年(37)

2008年(2)

分类: C/C++

2015-08-24 17:29:24

Poco::Exception:
    Poco::Exception 是所有Poco异常的父类,Poco::Exception由std::exception派生
    #include "Poco/Exception.h"

自定义 Exceptions:
抛出捕获异常:
    poco提供2个宏完成自定义异常声明和初始化工作
    POCO_DECLARE_EXCEPTION
    POCO_IMPLEMENT_EXCEPTION

点击(此处)折叠或打开

  1. #define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \
  2.     POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, 0)
  3. #define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME)                                                    \
  4.     CLS::CLS(int code): BASE(code)                                                                    \
  5.     {                                                                                                \
  6.     }                                                                                                \
  7.     CLS::CLS(const std::string& msg, int code): BASE(msg, code)                                        \
  8.     {                                                                                                \
  9.     }                                                                                                \
  10.     CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code)        \
  11.     {                                                                                                \
  12.     }                                                                                                \
  13.     CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code)    \
  14.     {                                                                                                \
  15.     }                                                                                                \
  16.     CLS::CLS(const CLS& exc): BASE(exc)                                                                \
  17.     {                                                                                                \
  18.     }                                                                                                \
  19.     CLS::~CLS() throw()                                                                                \
  20.     {                                                                                                \
  21.     }                                                                                                \
  22.     CLS& CLS::operator = (const CLS& exc)                                                            \
  23.     {                                                                                                \
  24.         BASE::operator = (exc);                                                                        \
  25.         return *this;                                                                                \
  26.     }                                                                                                \
  27.     const char* CLS::name() const throw()                                                            \
  28.     {                                                                                                \
  29.         return NAME;                                                                                \
  30.     }                                                                                                \
  31.     const char* CLS::className() const throw()                                                        \
  32.     {                                                                                                \
  33.         return typeid(*this).name();                                                                \
  34.     }                                                                                                \
  35.     Poco::Exception* CLS::clone() const                                                                \
  36.     {                                                                                                \
  37.         return new CLS(*this);                                                                        \
  38.     }                                                                                                \
  39.     void CLS::rethrow() const                                                                        \
  40.     {                                                                                                \
  41.         throw *this;                                                                                \
  42.     }

点击(此处)折叠或打开

  1. #define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME)                                                    \
  2.     CLS::CLS(int code): BASE(code)                                                                    \
  3.     {                                                                                                \
  4.     }                                                                                                \
  5.     CLS::CLS(const std::string& msg, int code): BASE(msg, code)                                        \
  6.     {                                                                                                \
  7.     }                                                                                                \
  8.     CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code)        \
  9.     {                                                                                                \
  10.     }                                                                                                \
  11.     CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code)    \
  12.     {                                                                                                \
  13.     }                                                                                                \
  14.     CLS::CLS(const CLS& exc): BASE(exc)                                                                \
  15.     {                                                                                                \
  16.     }                                                                                                \
  17.     CLS::~CLS() throw()                                                                                \
  18.     {                                                                                                \
  19.     }                                                                                                \
  20.     CLS& CLS::operator = (const CLS& exc)                                                            \
  21.     {                                                                                                \
  22.         BASE::operator = (exc);                                                                        \
  23.         return *this;                                                                                \
  24.     }                                                                                                \
  25.     const char* CLS::name() const throw()                                                            \
  26.     {                                                                                                \
  27.         return NAME;                                                                                \
  28.     }                                                                                                \
  29.     const char* CLS::className() const throw()                                                        \
  30.     {                                                                                                \
  31.         return typeid(*this).name();                                                                \
  32.     }                                                                                                \
  33.     Poco::Exception* CLS::clone() const                                                                \
  34.     {                                                                                                \
  35.         return new CLS(*this);                                                                        \
  36.     }                                                                                                \
  37.     void CLS::rethrow() const                                                                        \
  38.     {                                                                                                \
  39.         throw *this;                                                                                \
  40.     }
一个简单例子:

点击(此处)折叠或打开

  1. #include "Poco/Exception.h"
  2. #include <typeinfo>
  3. #include <stdio.h>

  4. #define MyExceptionAPI

  5. POCO_DECLARE_EXCEPTION(MyExceptionAPI, MyException, Poco::Exception)

  6. POCO_IMPLEMENT_EXCEPTION(MyException, Poco::Exception, "MyException")



  7. int throwExceptionFun()
  8. {
  9.         throw MyException("throwException");

  10.         return 0;
  11. }

  12. int main()
  13. {

  14.         try
  15.         {
  16.                 throwExceptionFun();
  17.         }
  18.         catch (MyException &e)
  19.         {
  20.                 printf("catch exception:%s\n", e.message().c_str());
  21.                 //message从Poco::Exception 继承,输出异常消息字符串"throwException"
  22.         }
  23.         return 0;
  24. }

Debugging Utilities:
阅读(3349) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~