Standard Exception Classes
时间:2010-05-27 来源:lxs_lover520
-
Exceptions for language support
-
Exceptions for the C++ standard library
-
Exceptions for errors outside the scope of a program
Exception Classes for Language Support
Exceptions for language support are used by language features. So in a way they are part of the core language rather than the library. These exceptions are thrown when the following operations fail.
-
An exception of class bad_alloc is thrown whenever the global operator new fails (except when the nothrow version of new is used). This is probably the most important exception because it might occur at any time in any nontrivial program.
-
An exception of class bad_cast is thrown by the dynamic_cast operator if a type conversion on a reference fails at runtime. The dynamic_cast operator is described on page 19.
-
An exception of class bad_typeid is thrown by the typeid operator for runtime type identification. If the argument to typeid is zero or the null pointer, this exception gets thrown.
-
An exception of class bad_exception is used to handle unexpected exceptions. It does this by using the function unexpected(). unexpected() is called if a function throws an exception that is not listed in an exception specification (exception specifications are introduced on page 16). For example:
class El; class E2; void f() throw(E1) //throws only exceptions of type E1 { ... throw El(); //throws exception of type El ... throw E2(); //calls unexpected(), which calls terminate() }
The throw of an exception of type E2 in f() violates the exception specification. In this case, the global function unexpected() gets called, which usually calls terminate() to terminate the program. However, if class bad_exception is part of the exception specification, then unexpected() usually rethrows an exception of this type:
class El; class E2; void f() throw(E1, std::bad_exception) //throws exception of type El or //bad_exception for any other exception type { ... throw El(); //throws exception of type El ... throw E2(); //calls unexpected(), which throws bad_exception }
Thus, if an exception specification includes the class bad_exception, then any exception not part of the specification may be replaced by bad_exception within the function unexpected().[1]
[1] You can modify the exact behavior of unexpected(). However, a function never throws exceptions other than those stated in its exception specification (if any).
Exception Classes for the Standard Library
Exception classes for the C++ standard library are usually derived from class logic_error. Logic errors are errors that, at least in theory, could be avoided by the program; for example, by performing additional tests of function arguments. Examples of such errors are a violation of logical preconditions or a class invariant. The C++ standard library provides the following classes for logic errors:
-
An exception of class invalid_argument is used to report invalid arguments, such as when a bitset (array of bits) is initialized with a char other than '0' or '1'.
-
An exception of class length_error is used to report an attempt to do something that exceeds a maximum allowable size, such as appending too many characters to a string.
-
An exception of class out_of_range is used to report that an argument value is not in the expected range, such as when a wrong index is used in an array-like collection or string.
-
An exception of class domain_error is used to report a domain error.
In addition, for the I/O part of the library, a special exception class called ios_base::failure is provided. It may be thrown when a stream changes its state due to an error or end-of-file. The exact behavior of this exception class is described in Section 13.4.4.
Exception Classes for Errors Outside the Scope of a Program
Exceptions derived from runtime_error are provided to report events that are beyond the scope of a program and are not easily avoidable. The C++ standard library provides the following classes for runtime errors:
-
An exception of class range_error is used to report a range error in internal computations.
-
An exception of class overflow_error is used to report an arithmetic overflow.
-
An exception of class underflow_error is used to report an arithmetic underflow.
Exceptions Thrown by the Standard Library
The C++ standard library itself can produce exceptions of classes range_error, out_of_range, and invalid_argument. However, because language features as well as user code are used by the library, their functions might throw any exception indirectly. In particular, bad_alloc exceptions can be thrown whenever storage is allocated.
Any implementation of the standard library might offer additional exception classes (either as siblings or as derived classes). However, the use of these nonstandard classes makes code non-portable because you could not use another implementation of the standard library without breaking your code. So, you should always use only the standard exception classes.
Header Files for Exception Classes
The base class exception and class bad_exception are defined in <exception>. Class bad_alloc is defined in <new>. Classes bad_cast and bad_typeid are defined in <typeinfo>. Class ios_base::failure is defined in <ios>. All other classes are defined in <stdexcept>.