C++ provides a list of standard exceptions defined in header <exception> in namespace std where “exception” is the base class for all standard exceptions. All exceptions like bad_alloc, bad_cast, runtime_error, etc generated by the standard library inherit from std::exception. Therefore, all standard exceptions can be caught by reference.
Below are the errors are thrown in C++:
|bad_alloc||thrown by new on allocation failure|
|bad_cast||thrown by dynamic_cast when it fails in a dynamic cast|
|bad_exeption||thrown by certain dynamic exception specifiers|
|bad_typeid||thrown by typeid|
|bad_function_call||thrown by empty function objects|
|bad_weak_ptr||thrown by shared_ptr when passed a bad weak_ptr|
|logic_error||error related to the internal logic of the program|
|runtime_error||error related to the internal logic of the program|
Below is the program to illustrate some of the errors in exception class in C++:
Below is the illustration of std::bad_alloc error:
using class bad_alloc
using class exception
Array created successfully of length 1000 std::bad_alloc for array of length 1000000000
- For creating an array of length 1000 the memory allocation was successful and there was no exception thrown for the same.
- For creating an array of length 1000 the memory allocation was not successful and the exception “std::bad_alloc” was thrown. The exception thrown is of type bad_alloc which is derived from the class exception. The function what() is a virtual function defined in the base class exception. The function what() returns a null terminated string which is generally a description of error.
Why did we catch an exception by reference?
Catching an exception by value will call the copy constructor and create a copy of the exception which adds run-time overhead. Thus, catching by reference is a better option. If we want to modify exception or add some additional information to the error message then catching by reference is best for it. For this case:
The above program wants to catch the exception, add some information to it and re-throw it. But str is a call by value variable which gets changed locally in the function and when the function re-throw the exception then the original exception is passed.
Below is the program to illustrate the logic_error:
Factorial of 0 is 1 Factorial of 3 is 6 negative not allowed
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- exception::what() in C++ with Examples
- exception::bad_exception in C++ with Examples
- Header files in C/C++ with Examples
- Exception Handling in C++
- Header files in C/C++ and its uses
- C++ | Exception Handling | Question 10
- C++ | Exception Handling | Question 9
- C++ | Exception Handling | Question 8
- C++ | Exception Handling | Question 7
- C++ | Exception Handling | Question 6
- C++ | Exception Handling | Question 5
- C++ | Exception Handling | Question 11
- Handling the Divide by Zero Exception in C++
- random header in C++ | Set 3 (Distributions)
- C++ | Exception Handling | Question 12
- clocale header file in C++
- random header in C++ | Set 1(Generators)
- C++ | Exception Handling | Question 2
- C++ | Exception Handling | Question 1
- random header | Set 2 (Distributions)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet