How to Throw and Catch Exceptions in C++?
Last Updated :
03 Apr, 2024
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 occurs and can be "caught" and "handled" to ensure the program's flow remains uninterrupted.
Throwing Exceptions in C++
We can use the throw keyword to throw an exception followed by an exception object from inside a try block. As soon as a program encounters a throw statement it immediately terminates the current function and starts finding a matching catch
block to handle the thrown exception.
Catching Exceptions in C++
To catch an exception, we can use the catch
keyword. The catch
block follows the try
block and is used to handle any exceptions that are thrown within the try
block.
Syntax to Throw and Catch Exceptions in C++
try {
// Code that might throw an exception
throw exception_object;
}
catch (exception_type e) {
//catch the exception thrown from try block and handle it
}
Here, exception_object
is an instance of an exception class, and exception_type
is the type of exception that the catch
block can handle.
C++ Program to Throw and Catch Exception
The following program illustrates how we can throw and catch an exception in C++.
C++
// C++ Program to illustrates how we can throw and catch an
// exception
#include <iostream>
#include <stdexcept>
using namespace std;
// Function to perform division of two numbers
void divide(int num1, int num2)
{
if (num2 == 0) {
// Throw a runtime_error exception if the
// denominator is zero
throw runtime_error("Division by zero error");
}
cout << "Result of division: " << num1 / num2 << endl;
}
int main()
{
// Declare two numbers
int num1 = 5;
int num2 = 0;
try {
// Perform Division
divide(num1, num2);
}
catch (const exception& e) {
// Catch the exception and print the error message
cerr << "Caught exception: " << e.what() << endl;
}
return 0;
}
Output
Caught exception: Division by zero error
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
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 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
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 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
How to Use the Try and Catch Blocks in C++? In C++, the try and catch blocks are used as a part of the exception handling mechanism which allows us to handle runtime errors. If the exceptions are not handled properly then the flow of the program is interrupted and the execution might fail. In this article, we will learn how to use try and cat
2 min read