Exception Header in C++ With Examples
Last Updated :
09 Oct, 2022
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.
Header File:
#include <exception>
Below are the errors thrown in C++:
Exceptions |
Description |
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 the exception class in C++
Program 1: Below is the illustration of the std::bad_alloc error:
using class bad_alloc
#include <exception>
#include <iostream>
using namespace std;
void createArray( int N)
{
try {
int * array = new int [N];
cout << "Array created successfully"
<< " of length " << N << " \n" ;
}
catch (bad_alloc& e) {
cout << e.what()
<< " for array of length "
<< N << " \n" ;
}
}
int main()
{
createArray(1000);
createArray(1000000000);
return 0;
}
|
using class exception
#include <exception>
#include <iostream>
using namespace std;
void createArray( int N)
{
try {
int * array = new int [N];
cout << "Array created successfully"
<< " of length " << N << " \n" ;
}
catch (exception& e) {
cout << e.what()
<< " for array of length "
<< N << " \n" ;
}
}
int main()
{
createArray(1000);
createArray(1000000000);
return 0;
}
|
Output
Array created successfully of length 1000
Array created successfully of length 1000000000
Explanation:
- 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 an error.
Note: bad_alloc exception is thrown by operator “new” when memory allocation fails.
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 an exception or add some additional information to the error message then catching by reference is best for it. For this case:
catch (std::string s)
{
s += "Additional info";
throw;
}
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 that gets changed locally in the function and when the function re-throws the exception then the original exception is passed.
Correct Code:
catch (std::string& s)
{
s += "Additional info";
throw;
}
Program 2: Below is the program to illustrate the logic_error:
CPP
#include <exception>
#include <iostream>
#include <stdexcept>
using namespace std;
void findFactorial( int N)
{
int factorial = 1;
try {
if (N < 0) {
throw invalid_argument( "negative not allowed" );
}
for ( int i = N; i > 0; i--) {
factorial *= i;
}
cout << "Factorial of " << N << " is " << factorial
<< endl;
}
catch (exception& e) {
cout << e.what();
}
}
int main()
{
findFactorial(0);
findFactorial(3);
findFactorial(-1);
return 0;
}
|
Output
Factorial of 0 is 1
Factorial of 3 is 6
negative not allowed
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...