Exception header in C++ with examples - GeeksforGeeks

16 Jul, 2020
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 are thrown in C++:
Below is the program to illustrate some of the errors in exception class in C++:
Program 1:Below is the illustration of std::bad_alloc error:
using class bad_alloc
using class exception
// C++ program to illustrate bad_alloc// using class bad_alloc#include <exception>#include <iostream>using namespace std;  // Function to illustrate bad_allocvoid createArray(int N){    // Try Block    try {        // Create an array of length N        int* array = new int[N];          // If created successfully then        // print the message        cout << "Array created successfully"             << " of length " << N << " \n";    }      // Check if the memory    // was allocated or not    // using class bad_alloc    catch (bad_alloc& e) {          // If not, print the error message        cout << e.what()             << " for array of length "             << N << " \n";    }}  // Driver Codeint main(){    // Function call to create an    // array of 1000 size    createArray(1000);      // Function call to create an    // array of 1000000000 size    createArray(1000000000);    return 0;}
// C++ program to illustrate bad_alloc// using class exception#include <exception>#include <iostream>using namespace std;  // Function to illustrate bad_allocvoid createArray(int N){    // Try Block    try {        // Create an array of length N        int* array = new int[N];          // If created successfully then        // print the message        cout << "Array created successfully"             << " of length " << N << " \n";    }      // Check if the memory    // was allocated or not    // using class exception    catch (exception& e) {          // If not, print the error message        cout << e.what()             << " for array of length "             << N << " \n";    }}  // Driver Codeint main(){    // Function call to create an    // array of 1000 size    createArray(1000);      // Function call to create an    // array of 1000000000 size    createArray(1000000000);    return 0;}
Array created successfully of length 1000 
std::bad_alloc for array 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 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 exception or add some additional information to the error message then catching by reference is best for it. For this case:
catch (std::string str){    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 which gets changed locally in the function and when the function re-throw 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:
// C++ program to illustrate logic_error#include <exception>#include <iostream>using namespace std;  // Function to find factorial of N and// throws error if occursvoid findFactorial(int N){    // Initialise variable by 1    int factorial = 1;      // Check for errors    try {        // If N is less than zero then,        // it shows errors as factorial        // of negative number can't be        // calculated        if (N < 0) {              // Exception object which            // returns the message passed            // to it            throw invalid_argument(                "negative not allowed");        }          // Find factorial if no error occurs        for (int i = N; i > 0; i--) {            factorial *= i;        }        cout << "Factorial of " << N             << " is " << factorial << endl;    }      // Print the error message    catch (exception& e) {        cout << e.what();    }}  // Driver Codeint main(){    // Function call to find factorial    // of 0    findFactorial(0);      // Function call to find factorial    // of 3    findFactorial(3);      // Function call to find factorial    // of -1    findFactorial(-1);      return 0;}
Factorial of 0 is 1
Factorial of 3 is 6
negative not allowed

nidhi_biet
C++-Exception Handling
cpp-exception
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Iterators in C++ STL
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Inline Functions in C++
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
C++ program for hashing with chaining
delete keyword in C++