How to Catch a Specific Exception in C++? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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++, the exception handling is done using try-catch statements. The basic syntax of the try-catch method is: try { // Code that might throw an exception throw SomeExceptionType("Error message"); } catch( SomeExceptionType e1 ) { // catch block catches the exception that is thrown from try block } To catch an exception of a specific type that is thrown in the try block, we have to mention its type in the catch block along with some name assigned to it. C++ Program to Catch a Specific Exception In this program, we will catch the divide-by-zero exception using the try-catch blocks. C++ // C++ program to catch divide by zero exception #include <iostream> #include <stdexcept> using namespace std; int main() { int x = 5; int y = 0; // write the code that may throw an exception try { if (y == 0) { // throw error throw runtime_error("Divide by zero error"); } int z = x / y; cout << "Result: " << z << endl; } // catch the specific exception catch (const runtime_error& e) { cout << "Exception Caught: " << e.what() << endl; } return 0; } OutputException Caught: Divide by zero error Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Catch a Specific Exception in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-exception C++-Exception Handling C++ Errors CPP Examples +2 More Practice Tags : CPP Similar Reads 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 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 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 Floating Point Errors in C++? In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at 2 min read 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 Exception Header in C++ With Examples 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 s 5 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 How to Read a File Character by Character in C++? In C++, file handling is used to store data permanently in a computer. Using file handling we can store our data in secondary memory (Hard disk). In this article, we will learn how to read a file character by character in C++. Example: Input: "Geeks for Geeks"Output: G e e k s f o r G e e k sRead Te 2 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read Like