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
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
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 {
public:
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:

exception

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.

logic_error

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

runtime_error

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

domain_error

Reports violations of a precondition.

invalid_argument

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

length_error

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).

out_of_range

Reports an out-of-range argument.

bad_cast

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

bad_typeid

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

range_error

Reports violation of a postcondition.

overflow_error

Reports an arithmetic overflow.

bad_alloc

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