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

Standard exceptions

The exceptions used with the Standard C++ library are also available for your use. Generally it s easier and faster to start with a standard exception class than to try to define your own. If the standard class doesn t do exactly what you need, you can derive from it.

All standard exception classes derive ultimately from the class exception, defined in the header <exception>. The two main derived classes are logic_error and runtime_error, which are found in <stdexcept> (which itself includes <exception>). The class logic_error represents errors in programming logic, such as passing an invalid argument. Runtime errors are those that occur as the result of unforeseen forces such as hardware failure or memory exhaustion. Both runtime_error and logic_error provide a constructor that takes a std::string argument so that you can store a message in the exception object and extract it later with exception::what( ) , as the following program illustrates:

//: C01:StdExcept.cpp
// Derives an exception class from std::runtime_error.
#include <stdexcept>
#include <iostream>
using namespace std;
class MyError : public runtime_error {
MyError(const string& msg = "") : runtime_error(msg) {}
int main() {
try {
throw MyError("my message");
} catch(MyError& x) {
cout << x.what() << endl;
} ///:~

Although the runtime_error constructor inserts the message into its std::exception subobject, std::exception does not provide a constructor that takes a std::string argument. You ll usually want to derive your exception classes from either runtime_error or logic_error (or one of their derivatives), and not from std::exception.

The following tables describe the standard exception classes:


The base class for all the exceptions thrown by the C++ Standard library. You can ask what( ) and retrieve the optional string with which the exception was initialized.


Derived from exception. Reports program logic errors, which could presumably be detected by inspection.


Derived from exception. Reports runtime errors, which can presumably be detected only when the program executes.


The iostream exception class ios::failure is also derived from exception, but it has no further subclasses.

You can use the classes in both of the following tables as they are, or you can use them as base classes from which to derive your own more specific types of exceptions.

Exception classes derived from logic_error


Reports violations of a precondition.


Indicates an invalid argument to the function from which it is thrown.


Indicates an attempt to produce an object whose length is greater than or equal to npos (the largest representable value of context s size type, usually std::size_t).


Reports an out-of-range argument.


Thrown for executing an invalid dynamic_cast expression in runtime type identification (see Chapter 8).


Reports a null pointer p in an expression typeid(*p). (Again, a runtime type identification feature in Chapter 8).


Exception classes derived from runtime_error


Reports violation of a postcondition.


Reports an arithmetic overflow.


Reports a failure to allocate storage.

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

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