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 this std::exception class. These are:
exception | description |
---|---|
bad_alloc | thrown by new on allocation failure |
bad_cast | thrown by dynamic_cast when fails with a referenced type |
bad_exception | thrown when an exception type doesn't match any catch |
bad_typeid | thrown by typeid |
ios_base::failure | thrown 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](https://i-blog.csdnimg.cn/blog_migrate/1db33ad04885172df93e88b97af9d872.jpeg)
Here is the small description of each exception mentioned in the above hierarchy:
Exception | Description |
---|---|
std::exception | An exception and parent class of all the standard C++ exceptions. |
std::bad_alloc | This can be thrown by new. |
std::bad_cast | This can be thrown by dynamic_cast. |
std::bad_exception | This is useful device to handle unexpected exceptions in a C++ program |
std::bad_typeid | This can be thrown by typeid. |
std::logic_error | An exception that theoretically can be detected by reading the code. |
std::domain_error | This is an exception thrown when a mathematically invalid domain is used |
std::invalid_argument | This is thrown due to invalid arguments. |
std::length_error | This is thrown when a too big std::string is created |
std::out_of_range | This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[](). |
std::runtime_error | An exception that theoretically can not be detected by reading the code. |
std::overflow_error | This is thrown if a mathematical overflow occurs. |
std::range_error | This is occured when you try to store a value which is out of range. |
std::underflow_error | This 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 caught C++ 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.