Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

When not to use exception specifications

If you peruse the function declarations throughout the Standard C++ library, you ll find that not a single exception specification occurs anywhere! Although this might seem strange, there is a good reason for this seeming incongruity: the library consists mainly of templates, and you never know what a generic type or function might do. For example, suppose you are developing a generic stack template and attempt to affix an exception specification to your pop function, like this:

T pop() throw(logic_error);

Since the only error you anticipate is a stack underflow, you might think it s safe to specify a logic_error or some other appropriate exception type. But type T s copy constructor could throw an exception. Then unexpected( ) would be called, and your program would terminate. You can t make unsupportable guarantees. If you don t know what exceptions might occur, don t use exception specifications. That s why template classes, which constitute the majority of the Standard C++ library, do not use exception specifications they specify the exceptions they know about in documentation and leave the rest to you. Exception specifications are mainly for non-template classes.

Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire