读书人

POCO C++库学习和分析 - 错误、异常处

发布时间: 2013-04-12 18:33:12 作者: rapoo

POCO C++库学习和分析 -- 异常、错误处理、调试
POCO C++库学习和分析 -- 异常、错误处理、调试


1. 异常处理 C++同C语言相比,提供了异常机制。通过使用try,catch关键字可以捕获异常,这种机制使得程序员在程序异常发生时,可以通过判断异常类型,来决定程序是否继续执行,并在程序结束之前优雅的释放各类资源。当然对于C++的异常机制也存在着很多的争议。在这里,并不对此展开讨论,只介绍一下Poco中的异常类。


Poco中的异常类:
1. 所有的异常类都是Poco::Exception的子类。
2. Poco::Exception继承自std::exception类。
3. Foundation库中涉及的异常类,包括了下面一些:
a) Poco::LogicException类负责处理程序错误,包括了:
AssertionViolationException
NullPointerException
NullValueException
BugcheckException
InvalidArgumentException
NotImplementedException
RangeException
IllegalStateException
InvalidAccessException
SignalException
UnhandledException
b) Poco::ApplicationException类负责处理应用程序相关的错误,即使用Poco库的用户自定义异常。
c) Poco::RuntimeException类负责处理程序运行时的错误,包括了:
RuntimeException
NotFoundException
ExistsException
TimeoutException
SystemException
RegularExpressionException
LibraryLoadException
LibraryAlreadyLoadedException
NoThreadAvailableException
PropertyNotSupportedException
PoolOverflowException
NoPermissionException
OutOfMemoryException
DataException
DataFormatException
SyntaxException
CircularReferenceException
PathSyntaxException
IOException
ProtocolException
FileException
FileExistsException
FileNotFoundException
PathNotFoundException
FileReadOnlyException
FileAccessDeniedException
CreateFileException
OpenFileException
WriteFileException
ReadFileException
UnknownURISchemeException


成员函数及数据定义:
1. Poco::Exception包括了一个名字,这是一个静态的字符串,用来描述异常本身。比如说LogicException名字为"Logic exception",TimeoutException名字为"Timeout"。
2. Poco::Exception还包含了一个字符串消息,这是用来进一步描述异常的。使用的的人可以在运行时定义它。比如都是LogicException异常,函数一处抛出异常时可定义为"Function1",函数二处抛出时异常时可定义为用"Function2",它可以用来说明异常发生的具体位置和原因。
3. 一个可选的嵌套异常类
4. 构造函数:
a) 可以使用0个,1个或2个字符串参数来构造异常。在Poco::Exception内部存储的时候,第二个字符串会使用字符":"和第一个字符串串联。
b) 构造时如果使用了字符串和嵌套异常的方式,嵌套异常会被复制一份。
5. Poco::Exception支持拷贝和赋值运算符
6. const char* name()
返回异常的名称
7. const std::string& message()
返回在构造时传入的消息字符串
8. std::string displayText() const
同时返回异常名字和消息字符串,中间使用": "分隔
9. const Exception* nested() const
如果存在嵌套异常的话,返回之歌指向嵌套异常的指针,否则返回0
10. Exception* clone() const
返回一个异常的拷贝
11. void rethrow() const
重新抛出异常


定义自己的异常:
因为从Poco::Exception继承,去定义自己的异常时,工作非常的枯燥且重复(用户需要重载大量的虚函数),在库中提供了两个宏来完成这个工作:
POCO_DECLARE_EXCEPTION:用来申明异常宏
POCO_IMPLEMENT_EXCEPTION: 用来定义异常宏的执行体

两个宏分别定义如下:




CurrentThreadHolder类是TLS实现的具体类,在每个Thread对象中包含了一个CurrentThreadHolder对象。Thread创建的时候,CurrentThreadHolder会调用不同操作系统的API函数,获取并保存一个固定槽位,用于保存Thread对象的指针。
每个Thread对象中还包含了一个ThreadLocalStorage对象。ThreadLocalStorage类用于保存具体的线程信息数据,它是一个TLSSlot对象的集合。通过泛型实现TLSSlot后,ThreadLocalStorage可用于保存任何数据的。

使用了TLS技术后,调用Thread的静态函数current可以获取到每个线程对象Thread的指针,然后再通过这个Thread对象的指针,可以获取到ThreadLocalStorage对象,并最终获取或保存数据于TLSSlot中。

通过类的静态函数获取类实例的指针,在C++中是不存在的,这需要操作系统支持,只有Thread对象才能做到这一点。



3.2.2 NDC 在来看一张Poco中NDC类的类图:

POCO C++库学习和分析 - 错误、异常处理、调试



使用者通过调用宏poco_ndc和poco_ndc_dbg,来构建一个NDCScope对象。宏定义如下:
template <class C>class ThreadLocal/// This template is used to declare type safe thread/// local variables. It can basically be used like/// a smart pointer class with the special feature/// that it references a different object/// in every thread. The underlying object will/// be created when it is referenced for the first/// time./// See the NestedDiagnosticContext class for an/// example how to use this template./// Every thread only has access to its own/// thread local data. There is no way for a thread/// to access another thread's local data.{typedef TLSSlot<C> Slot;public:ThreadLocal(){}~ThreadLocal(){}C* operator -> (){return &get();}C& operator * ()/// "Dereferences" the smart pointer and returns a reference/// to the underlying data object. The reference can be used/// to modify the object.{return get();}C& get()/// Returns a reference to the underlying data object./// The reference can be used to modify the object.{TLSAbstractSlot*& p = ThreadLocalStorage::current().get(this);if (!p) p = new Slot;return static_cast<Slot*>(p)->value();}private:ThreadLocal(const ThreadLocal&);ThreadLocal& operator = (const ThreadLocal&);};

到这里Poco中所有的NDC流程都被打通了,用户终于可以实现按线程打印日志信息了。


(版权所有,转载时请注明作者和出处 http://blog.csdn.net/arau_sh/article/details/8698353)



读书人网 >C++

热点推荐