C + + standard Library Exception class2012-12-24 16:27 5269 People read Comments (1) favorite reports Classification:C + + (+)
The root class in the C + + standard library Exception class inheritance hierarchy is exception, which is defined in the exception header file, which is the base class in which all functions of the C + + standard library throw exceptions, and the interface definition for exception is as follows:
Namespace Std {
Class Exception {
Public
Exception () throw (); Do not throw any exceptions
Exception (const exception& e) throw ();
exception& operator= (const exception& e) throw ();
Virtual ~exception () throw) ();
Virtual Const char* What () const throw (); Returns the description of the exception
};
}
In addition to the exception class, C + + provides classes for reporting cases where the program is unhealthy, and in the error model that is reflected in these predefined classes, there are two main categories of logic errors and run-time errors.
Logic errors mainly include Invalid_argument, Out_of_range, Length_error, Domain_error. When a function receives an invalid argument, it throws a Invaild_argument exception, and if the function receives an argument that exceeds the expected range, it throws a Out_of_range exception, and so on.
Namespace Std {
Class Logic_error:public Exception {
Public
Explicit Logic_error (const string &what_arg);
};
Class Invalid_argument:public Logic_error {
Public
Explicit invalid_argument (const string &what_arg);
};
Class Out_of_range:public Logic_error {
Public
Explicit Out_of_range (const string &what_arg);
};
Class Length_error:public Logic_error {
Public
Explicit Length_error (const string &what_arg);
};
Class Domain_error:public Logic_error {
Public
Explicit Domain_error (const string &what_arg);
};
}
Run-time errors are raised by events outside the program domain and can be detected only at run time, mainly including Range_error, Overflow_error, Underflow_error. A function can report an overflow error by throwing a overflow_error by throwing Range_eroor to report a range error in arithmetic operations.
Namespace Std {
Class Runtime_error:public Exception {
Public
Explicit Runtime_error (const string &what_arg);
};
Class Range_error:public Runtime_error {
Public
Explicit Range_error (const string &what_arg);
};
Class Overflow_error:public Runtime_error {
Public
Explicit Overflow_error (const string &what_arg);
};
Class Underflow_error:public Runtime_error {
Public
Explicit Underflow_error (const string &what_arg);
};
}
In addition, the BAD_ALLOC exception is defined in the new header file, and exception is the base class for Bad_alloc, which reports that the new operator does not properly allocate memory. When dynamic_cast fails, the program throws the Bad_cast exception class, which also inherits from the exception class.
C + + Standard Library Exception class