C++ standard exceptions

来源:互联网 发布:windows更新后c盘变小 编辑:程序博客网 时间:2024/05/29 16:37

Standard exceptions

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called exception and is defined in the <exception> header file under the namespace std. This class has the usual default and copy constructors, operators and destructors, plus an additional virtual member function called what that returns a null-terminated character sequence (char *) and that can be overwritten in derived classes to contain some sort of description of the exception.

We have placed a handler that catches exception objects by reference (notice the ampersand & after the type), therefore this catches also classes derived from exception, like our myex object of class myexception.

All exceptions thrown by components of the C++ Standard library throw exceptions derived from thisstd::exception class. These are:

exceptiondescriptionbad_allocthrown by new on allocation failurebad_castthrown by dynamic_cast when fails with a referenced typebad_exceptionthrown when an exception type doesn't match any catchbad_typeidthrown by typeidios_base::failurethrown by functions in the iostream library

C++ provides a list of standard exceptions defined in <exception> which we can use in our programs. These are arranged in an a parent-child class hierarchy shown below:

C++ Exceptions Hierarchy

Here is the small description of each exception mentioned in the above hierarchy:

ExceptionDescriptionstd::exceptionAn exception and parent class of all the standard C++ exceptions.std::bad_allocThis can be thrown by new.std::bad_castThis can be thrown by dynamic_cast.std::bad_exceptionThis is useful device to handle unexpected exceptions in a C++ programstd::bad_typeidThis can be thrown by typeid.std::logic_errorAn exception that theoretically can be detected by reading the code.std::domain_errorThis is an exception thrown when a mathematically invalid domain is usedstd::invalid_argumentThis is thrown due to invalid arguments.std::length_errorThis is thrown when a too big std::string is createdstd::out_of_rangeThis can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().std::runtime_errorAn exception that theoretically can not be detected by reading the code.std::overflow_errorThis is thrown if a mathematical overflow occurs.std::range_errorThis is occured when you try to store a value which is out of range.std::underflow_errorThis is thrown if a mathematical underflow occurs.

Define New Exceptions:

You can define your own exceptions by inheriting and overriding exception class functionality. Following is the example which shows how you can use std::exception class to implement your own exception in standard way:

#include <iostream>#include <exception>using namespace std;struct MyException : public exception{  const char * what () const throw ()  {    return "C++ Exception";  }}; int main(){  try  {    throw MyException();  }  catch(MyException& e)  {    std::cout << "MyException caught" << std::endl;    std::cout << e.what() << std::endl;  }  catch(std::exception& e)  {    //Other errors  }}

This would produce following result:

MyException caughtC++ Exception

Here what() is a public method provided by exception class and it has been overridden by all the child exception classes. This returns the cause of an exception.


原创粉丝点击