Standard Exceptions
C++ provides a list of standard exceptions defined in <exception> which we can use in our programs. These are arranged in a parent-child class hierarchy shown below −
Here is the small description of each exception mentioned in the above hierarchy −
Sr.No | Exception & Description |
---|---|
1 | std::exceptionAn exception and parent class of all the standard C++ exceptions. |
2 | std::bad_allocThis can be thrown by new. |
3 | std::bad_castThis can be thrown by dynamic_cast. |
4 | std::bad_exceptionThis is useful device to handle unexpected exceptions in a C++ program. |
5 | std::bad_typeidThis can be thrown by typeid. |
6 | std::logic_errorAn exception that theoretically can be detected by reading the code. |
7 | std::domain_errorThis is an exception thrown when a mathematically invalid domain is used. |
8 | std::invalid_argumentThis is thrown due to invalid arguments. |
9 | std::length_errorThis is thrown when a too big std::string is created. |
10 | std::out_of_rangeThis can be thrown by the ‘at’ method, for example a std::vector and std::bitset<>::operator[](). |
11 | std::runtime_errorAn exception that theoretically cannot be detected by reading the code. |
12 | std::overflow_errorThis is thrown if a mathematical overflow occurs. |
13 | std::range_errorThis is occurred when you try to store a value which is out of range. |
14 | std::underflow_errorThis is thrown if a mathematical underflow occurs. |