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
// C++ program to illustrate bad_alloc
// using class bad_alloc
#include <exception>
#include <iostream>
using namespace std;
// Function to illustrate bad_alloc
void 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 Code
int 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;
}
using class exception
// C++ program to illustrate bad_alloc
// using class exception
#include <exception>
#include <iostream>
using namespace std;
// Function to illustrate bad_alloc
void 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 Code
int 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;
}
OutputArray 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
// C++ program to illustrate logic_error
#include <exception>
#include <iostream>
#include <stdexcept>
using namespace std;
// Function to find factorial of N and
// throws error if occurs
void 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 Code
int 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;
}
OutputFactorial of 0 is 1
Factorial of 3 is 6
negative not allowed
Similar Reads
Exception Handling using classes in C++ In C++, unexpected issues may occur during program execution such as attempting to divide by zero, accessing a non-existent file or using invalid data. These issues are called exceptions. These exceptions must be handled to avoid abnormal termination of the program.C++ provides try-catch block to ha
5 min read
How to Catch All Exceptions in C++? In C++, exceptions are objects that indicate you have an error in your program. They are handled by the try-catch block in C++. In this article, we will learn how to catch all the exceptions in C++. Catching All Exceptions in C++To catch all kinds of exceptions in our catch block in C++, we can defi
2 min read
C++ Program to Handle the Checked Exceptions An Exception is a run-time error, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. For example, Lack of Memory, Lack of Disk Space, Dividing by zero, etc. Types of Exceptions There are two types of Exceptions, Built-in Exceptions, and User-
5 min read
Helper Function in C++ Classes In C++, the users can keep their class methods organized and readable with the help of private helper functions. Helper functions are utility functions that are only accessible within the class. They can't be called externally but can be used by the class's public methods to perform lower-level task
3 min read
How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e
2 min read
How to Throw an Exception in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are objects that represent an error that occurs during the execution of a program. In this article, we will learn how to throw an exception in C++. Throw a C++ ExceptionThrowing an exception means sendin
2 min read
How to Throw and Catch Exceptions in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are unusual conditions that occur at runtime. In this article, we will learn how to throw and catch exceptions in C++. Throw and Catch Exceptions in C++In C++ exceptions can be "thrown" when an error occ
2 min read
How to Catch a Specific Exception in C++? In C++, exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. The process of handling these exceptions is called exception handling. In this article, we will learn how we can catch specific exceptions in C++. Catch a Specific Exception in C++ In C++,
2 min read
Life cycle of Objects in C++ with Example In Object Oriented Programming, Objects are the instances of a class which has its own state(variables) and behavior(methods). Every class has two special methods related with creation and destruction of object of a class- constructors and destructors. C++ Object Life Cycle: There are various steps
6 min read
C++ Program to Handle the Unchecked Exceptions Exceptions are run-time errors or abnormal conditions that a program may encounter during execution. Examples: Division by zeroAccess to an array out of its boundsRunning out of memoryRunning out of disk spaceTypes of Exceptions: Synchronous Exceptions: The exceptions which occur during the program
5 min read