How to Throw an Exception in C++? Last Updated : 03 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 sending the exception to the catch block after it has occurred in the program. To throw an exception in C++, we can use the throw keyword followed by an instance of the exception. When a program encounters a throw statement, then it immediately terminates the current function and starts finding a matching catch block to handle the thrown exception. Generally throw is used inside a try block or a function that is called within a try block. Syntax to Throw an Exception in C++throw exception_objectHere, exception_object is typically an instance of an exception class that can be a built-in type (like int or const char*), but more commonly, we use a class derived from std::exception. C++ Program to Throw an ExceptionThe following program illustrates how we can throw an exception in case of divide by zero runtime error in C++. C++ // C++ Program to illustrate how to throw an exception #include <iostream> #include <stdexcept> using namespace std; // Function to perform division void divide(int x, int y) { // Check if the denominator is zero if (y == 0) { // If denominator is zero, throw an exception of // type runtime_error throw runtime_error("Division by zero error"); } // If denominator is not zero, perform division and // print Result cout << "Result: " << x / y << endl; } int main() { try { divide(10, 0); } catch (const exception& e) { // Catch any exception thrown during the execution // of divide function cerr << "Exception caught: " << e.what() << endl; } return 0; } Output Exception caught: Division by zero errorTime Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Throw an Exception in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-exception C++-Exception Handling CPP Examples +1 More Practice Tags : CPP Similar Reads 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 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 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 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 C++ Program to Show Runtime Exceptions A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++. There are 5 types of runtime exceptions discussed here: Division by zero. Segmentation faults. La 3 min read Like