55. Pre-defined exceptions

By | September 27, 2021

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 −

C++ Exceptions Hierarchy

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

Sr.NoException & Description
1std::exceptionAn exception and parent class of all the standard C++ exceptions.
2std::bad_allocThis can be thrown by new.
3std::bad_castThis can be thrown by dynamic_cast.
4std::bad_exceptionThis is useful device to handle unexpected exceptions in a C++ program.
5std::bad_typeidThis can be thrown by typeid.
6std::logic_errorAn exception that theoretically can be detected by reading the code.
7std::domain_errorThis is an exception thrown when a mathematically invalid domain is used.
8std::invalid_argumentThis is thrown due to invalid arguments.
9std::length_errorThis is thrown when a too big std::string is created.
10std::out_of_rangeThis can be thrown by the ‘at’ method, for example a std::vector and std::bitset<>::operator[]().
11std::runtime_errorAn exception that theoretically cannot be detected by reading the code.
12std::overflow_errorThis is thrown if a mathematical overflow occurs.
13std::range_errorThis is occurred when you try to store a value which is out of range.
14std::underflow_errorThis is thrown if a mathematical underflow occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *