Library: Language support
Does not inherit
Class exception is the base class that supports logic and runtime errors.
#include <exception>
namespace std {
class exception;
}
namespace std {
class exception {
public:
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
};
}
//
// except.cpp
//
#include <iostream>
#include <stdexcept>
#ifndef _RWSTD_NO_EXCEPTIONS
int main ()
{
try {
// Enable exceptions in cin.
std::cin.exceptions(std::ios::eofbit);
// Clear all bits and set eofbit.
std::cin.clear(std::ios::eofbit);
}
catch (const std::ios::failure &e) {
std::cout << "Caught an exception: "
<< e.what () << std::endl;
}
catch (const std::exception &e) {
std::cout << "Caught an exception: "
<< e.what () << std::endl;
return 1; // Indicate failure.
}
catch (...) {
std::cout << "Caught an unknown exception"
<< std::endl;
return 1; // Indicate failure.
}
return 0;
}
#else
int main ()
{
std::cout << "Exceptions not supported." << std::endl;
return 0;
}
#endif // _RWSTD_NO_EXCEPTIONS
Program Output:
Caught an exception: std::cin: stream object has set ios::eofbit
exception() throw();
Default constructor. Constructs an object of class exception.
exception(const exception&) throw();
Copy constructor. Copies an exception object.
virtual ~exception() throw();
Destructor. Destroys an object of class exception.
exception& operator=(const exception&) throw();
Assignment operator. Copies an exception object.
virtual const char* what() const throw();
Returns an implementation-defined, null-terminated byte string representing a human-readable message describing the exception. The message may be a null-terminated multibyte string, suitable for conversion and display as a wstring.
Exceptions, bad_alloca, bad_cast, bad_exception, bad_typeid, domain_error, invalid_argument, ios_base::failure, length_error, logic_error, out_of_range, overflow_error, range_error, runtime_error, underflow_error
ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 19.1