{"id":25670,"date":"2023-07-17T12:22:41","date_gmt":"2023-07-17T06:52:41","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/"},"modified":"2024-09-03T11:08:21","modified_gmt":"2024-09-03T05:38:21","slug":"exception-handling-in-cpp","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/","title":{"rendered":"Exception handling in C++ | What is Exception handling in C++"},"content":{"rendered":"\n<p>Exception handling in C++ is a particular condition for developers to handle. In programming, committing mistakes that prompt unusual conditions called errors is normal. All in all, these errors are of three kinds:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Syntax Error<\/li>\n\n\n\n<li>Logical Error&nbsp;<\/li>\n\n\n\n<li>Runtime Error<\/li>\n<\/ol>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-exception-handling-in-c\"><strong>What is Exception Handling in C++?&nbsp;<\/strong><\/h2>\n\n\n\n<p>Exception handling in C++ is a mechanism that allows a program to deal with runtime errors and exceptional situations in a structured and controlled manner. In C++, exceptions are used to handle errors that occur during the execution of a program, such as division by zero, accessing invalid memory, or file I\/O errors.<\/p>\n\n\n\n<p>The basic idea behind exception handling is to separate the normal flow of program execution from error-handling code. Instead of terminating the program abruptly when an error occurs, C++ provides a way to \"throw\" an exception, representing the error or exceptional condition. The thrown exception is then caught by appropriate \"catch\" blocks, where the program can handle the error gracefully.<\/p>\n\n\n\n<p>Here's a basic outline of how exception handling works in C++:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Throwing an Exception:<\/strong><br>When a critical error occurs during program execution, you can use the <code>throw<\/code> statement to raise an exception. It usually takes an object as an argument, which serves as the representation of the error.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n\nvoid someFunction(int value) {\n    if (value == 0)\n        throw std::runtime_error(&quot;Error: Division by zero!&quot;);\n    \/\/ ... other code ...\n}\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Catching an Exception:<\/strong><br>To catch an exception, you use a <code>try<\/code> block. The code that might raise an exception is placed inside the <code>try<\/code> block. If an exception is thrown within the <code>try<\/code> block, the program will immediately jump to the corresponding <code>catch<\/code> block.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint main() {\n    try {\n        int x = 10;\n        int y = 0;\n        someFunction(x \/ y);\n    } catch (const std::runtime_error&amp; e) {\n        std::cout &lt;&lt; &quot;Exception caught: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n    \/\/ ... other code ...\n    return 0;\n}\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Handling the Exception:<\/strong><br>The <code>catch<\/code> block handles the caught exception. It specifies the type of exception it can catch in parentheses, followed by a block of code that handles the exceptional condition.<\/li>\n\n\n\n<li><strong>Multiple Catch Blocks:<\/strong><br>You can have multiple <code>catch<\/code> blocks to handle different types of exceptions. The first <code>catch<\/code> block that matches the thrown exception's type will be executed, and the others will be skipped.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ntry {\n    \/\/ code that may throw exceptions\n} catch (const SomeExceptionType&amp; e) {\n    \/\/ handle SomeExceptionType\n} catch (const AnotherExceptionType&amp; e) {\n    \/\/ handle AnotherExceptionType\n} catch (...) {\n    \/\/ handle any other exception that is not caught by previous catch blocks\n}\n<\/pre><\/div>\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Exception Safety:<\/strong><br>Exception safety refers to the concept of ensuring that a program's state remains consistent even if an exception is thrown. Writing exception-safe code is essential to prevent resource leaks and maintain data integrity.<\/li>\n<\/ol>\n\n\n\n<p>By using exception handling, you can make your C++ programs more robust and maintainable, as they provide a way to handle errors in a controlled manner, rather than having the program terminate abruptly on encountering an issue.<\/p>\n\n\n\n<p>When no exception condition happens, the code will execute ordinarily. The handlers will be disregarded.<\/p>\n\n\n\n<p><strong>A simple example to understand Exceptional handling in C++<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main() {\n    try {\n        \/\/ Code that may throw an exception\n        int numerator = 10;\n        int denominator = 0;\n        int result = numerator \/ denominator;\n\n        std::cout &lt;&lt; \"Result: \" &lt;&lt; result &lt;&lt; std::endl;\n    }\n    catch (const std::exception&amp; e) {\n        std::cout &lt;&lt; \"Exception occurred: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the division operation numerator\/denominator may throw a std::exception when the denominator is zero. The try block contains the code that might throw an exception, and the catch block catches the exception and handles it appropriately.<\/p>\n\n\n\n<p><strong><em><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/file-handling-in-c-in-hindi\" target=\"_blank\" rel=\"noreferrer noopener\">Also, now you can learn Exception Handling in C -  A Free Online Course in Hindi<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-exception-handling\"><strong>Why Exception Handling?&nbsp;<\/strong><\/h2>\n\n\n\n<p>Exception handling is a crucial concept in programming that allows developers to deal with unexpected or exceptional situations that may occur during the execution of a program. These exceptional situations are often referred to as \"exceptions.\" Here are some reasons why exception handling is important:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Error Management: When a program encounters an error or unexpected condition, without exception handling, it might crash or produce incorrect results. Exception handling provides a structured way to deal with errors and allows developers to handle them gracefully.<\/li>\n\n\n\n<li>Robustness: Exception handling enhances the robustness of the software. By catching and handling exceptions, developers can prevent the entire program from terminating abruptly and provide users with meaningful error messages, making the software more user-friendly.<\/li>\n\n\n\n<li>Separation of Concerns: Exception handling clearly separates normal program flow and error running code. This separation makes the code easier to read, understand, and maintain.<\/li>\n\n\n\n<li>Debugging: Exception handling aids in debugging the code. When an exception is thrown, the program can log details about the error, which helps developers identify and fix the issue's root cause.<\/li>\n\n\n\n<li>Graceful Recovery: In certain cases, programs can recover from exceptions and continue execution instead of crashing. For example, a web server can catch an exception caused by a client-side error and respond with an appropriate HTTP error code instead of shutting down.<\/li>\n\n\n\n<li>Program Stability: By handling exceptions appropriately, developers can ensure that the program remains stable and reliable even when facing unexpected conditions or inputs.<\/li>\n\n\n\n<li>Fail-Safe Operations: Exception handling is especially important when dealing with critical operations like file I\/O, network communication, or database transactions. Handling exceptions correctly makes it possible to roll back transactions or perform other necessary cleanup tasks to maintain data integrity.<\/li>\n\n\n\n<li>Modularity: Exception handling allows for modular design and promotes code reusability. Functions or methods can throw exceptions, and the calling code can catch and handle them accordingly.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"basic-keywords-in-exception-handling\"><strong>Basic Keywords in Exception Handling:&nbsp;<\/strong><\/h3>\n\n\n\n<p>Exception Handling in C++ falls around these three keywords:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Throw<\/li>\n\n\n\n<li>Catch<\/li>\n\n\n\n<li>Try<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-try-throw-catch-in-c\"><strong>What is try throw catch in c++?<\/strong><\/h3>\n\n\n\n<p>In C++, <code>try<\/code>, <code>throw<\/code>, and <code>catch<\/code> are keywords used for exception handling. Exception handling allows developers to handle errors or exceptional situations gracefully and provide a structured way to manage unexpected conditions during the execution of a program.<\/p>\n\n\n\n<p>Here's a brief explanation of each keyword:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>try: The code that might raise an exception is included within the try block.&nbsp;One or more catch blocks come after it. The program looks for a catch block that matches the try block when an exception is thrown within the try block in order to handle the exception. <\/li>\n\n\n\n<li>throw: To explicitly raise or throw an exception, use the throw keyword. In the try block, it is frequently employed when an exceptional circumstance arises. The control exits the try block when the throw statement is met in order to locate a suitable catch block to handle the exception.<\/li>\n\n\n\n<li><code>catch<\/code>: The <code>catch<\/code> block follows the <code>try<\/code> block and is used to catch and handle exceptions. It contains code that executes when a specific type of exception is thrown within the associated <code>try<\/code> block. Multiple <code>catch<\/code> blocks can be used for different exception types.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ntry {\n    \/\/ Code that might throw an exception\n    \/\/ If an exception is thrown, control jumps to the corresponding catch block\n} catch (ExceptionType1 e) {\n    \/\/ Code to handle ExceptionType1\n} catch (ExceptionType2 e) {\n    \/\/ Code to handle ExceptionType2\n} catch (...) {\n    \/\/ Catch-all block to handle any other unhandled exceptions (optional)\n}\n\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-try-catch-in-c-works\"><strong>How try-catch in c++ works?<\/strong><\/h3>\n\n\n\n<p>In C++, exception handling is done using the <code>try-catch<\/code> mechanism. It allows you to catch and handle exceptions that occur during the execution of your program. The <code>try<\/code> block contains the code that might throw an exception, and it handles the exception if it occurs. Here's how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The code that might throw an exception is enclosed within a <code>try<\/code> block. If an exception occurs within this block, the execution of the code within the <code>try<\/code> block is immediately stopped, and the program looks for a matching <code>catch<\/code> block to handle the exception.<\/li>\n\n\n\n<li>After an exception is thrown, the program searches for a matching <code>catch<\/code> block. A matching <code>catch<\/code> block is one that can handle the specific type of exception that was thrown. If a matching <code>catch<\/code> block is found, the code within that block is executed.<\/li>\n\n\n\n<li>If no matching <code>catch<\/code> block is found within the current scope, the program moves up the call stack, searching for an appropriate <code>catch<\/code> block in the calling functions. This process continues until a matching <code>catch<\/code> block is found or until the program reaches the top level of the program (i.e., <code>main()<\/code> function).<\/li>\n\n\n\n<li>Once a matching <code>catch<\/code> block is found, the code within that block is executed, and the program continues executing from the point immediately after the <code>try-catch<\/code> block.<\/li>\n<\/ol>\n\n\n\n<p><strong>Here's an example to illustrate the usage of <code>try-catch<\/code>:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main() {\n    try {\n        \/\/ Code that might throw an exception\n        int num1, num2;\n        std::cout &lt;&lt; \"Enter two numbers: \";\n        std::cin &gt;&gt; num1 &gt;&gt; num2;\n\n        if (num2 == 0) {\n            throw std::runtime_error(\"Divide by zero exception\");\n        }\n\n        int result = num1 \/ num2;\n        std::cout &lt;&lt; \"Result: \" &lt;&lt; result &lt;&lt; std::endl;\n    }\n    catch (const std::exception&amp; e) {\n        \/\/ Exception handling code\n        std::cout &lt;&lt; \"Exception caught: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Example1: Multiple Code Block <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main() {\n    try {\n        \/\/ Code that may throw an exception\n        int numerator = 10;\n        int denominator = 0;\n        int result = numerator \/ denominator;\n\n        std::cout &lt;&lt; \"Result: \" &lt;&lt; result &lt;&lt; std::endl;\n    }\n    catch (const std::runtime_error&amp; e) {\n        std::cout &lt;&lt; \"Runtime error occurred: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n    catch (const std::exception&amp; e) {\n        std::cout &lt;&lt; \"Exception occurred: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>Here, we have added an additional <code>catch<\/code> block to handle a specific type of exception, <code>std::runtime_error<\/code>, before catching the more general <code>std::exception<\/code>. The specific exception types should be caught before the more general ones.<\/p>\n\n\n\n<p><strong>Example2: Throwing a Custom Exception<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;stdexcept&gt;\n\nvoid checkAge(int age) {\n    if (age &lt; 0) {\n        throw std::invalid_argument(\"Age cannot be negative.\");\n    }\n    else if (age &lt; 18) {\n        throw std::out_of_range(\"You must be at least 18 years old.\");\n    }\n    else {\n        std::cout &lt;&lt; \"Access granted.\" &lt;&lt; std::endl;\n    }\n}\n\nint main() {\n    try {\n        int userAge = 15;\n        checkAge(userAge);\n    }\n    catch (const std::exception&amp; e) {\n        std::cout &lt;&lt; \"Exception occurred: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>checkAge<\/code> the function throws custom exceptions, <code>std::invalid_argument<\/code> and <code>std::out_of_range<\/code>, based on the age value provided. The <code>try<\/code> block calls the <code>checkAge<\/code> function, and if an exception is thrown, it is caught and handled in the <code>catch<\/code> block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-use-try-catch-in-c\"><strong>How to use try-catch in c++?<\/strong><\/h3>\n\n\n\n<p>Try-catch is an important keyword while performing exceptional conditions.<br>In the Try block, the \"throw\" keyword throws an exception when the code detects a problem, which lets us create a custom error. <br>Now \"catch\" keyword comes into a picture i.e. \"catch\" keyword allows you to define a block of code to be executed if an error occurs in the try block. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-do-you-catch-exceptions-in-c\"><strong>How do you catch exceptions in C++?<\/strong><\/h3>\n\n\n\n<p>To <strong>catch exceptions<\/strong>, a part of the code is kept under<strong> <\/strong>inspection. This is done by closing that part of the code in a try-block. When an exceptional circumstance arises within that block, an <strong>exception<\/strong> is thrown and an exception handler takes control over the program.  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-throw-an-exception-in-c\"><strong>How to throw an exception in c++?<\/strong><\/h3>\n\n\n\n<p>In C++, you can throw an exception using the throw keyword. Exceptions are a way to handle error conditions or exceptional situations in your code that may disrupt the normal flow of execution. When an exception is thrown, the program will stop executing the current block of code and start searching for an appropriate exception handler (catch block) to handle the exception.<\/p>\n\n\n\n<p>To throw an exception in C++, you typically follow these steps:<\/p>\n\n\n\n<p>Define a custom exception class (optional):<br>You can create your own custom exception class by inheriting from the standard std::exception class or any of its derived classes. This step is optional, as you can also use the standard exception classes provided by the C++ Standard Library.<\/p>\n\n\n\n<p>Throw the exception:<br>Use the throw keyword followed by the exception object you want to throw. If you have created a custom exception class, you can instantiate an object of that class and pass it to the throw statement.<\/p>\n\n\n\n<p>Catch the exception (optional):<br>To handle the thrown exception, you need to enclose the code that may throw an exception within a try-catch block. The catch block will catch the thrown exception and allow you to handle it gracefully.<\/p>\n\n\n\n<p>Here's an example of how to throw and catch an exception in C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\n\/\/ Custom exception class (optional)\nclass MyException : public std::exception {\npublic:\n    virtual const char* what() const noexcept override {\n        return \"My custom exception occurred!\";\n    }\n};\n\nint main() {\n    try {\n        int age;\n        std::cout &lt;&lt; \"Enter your age: \";\n        std::cin &gt;&gt; age;\n\n        if (age &lt; 0) {\n            \/\/ Throw a custom exception\n            throw MyException();\n        }\n\n        \/\/ Other code that may throw exceptions\n        \/\/ ...\n\n    } catch (const MyException&amp; ex) {\n        std::cout &lt;&lt; \"Caught custom exception: \" &lt;&lt; ex.what() &lt;&lt; std::endl;\n    } catch (const std::exception&amp; ex) {\n        \/\/ Catch other exceptions derived from std::exception\n        std::cout &lt;&lt; \"Caught standard exception: \" &lt;&lt; ex.what() &lt;&lt; std::endl;\n    } catch (...) {\n        \/\/ Catch any other unhandled exceptions (not recommended, but can be useful for debugging)\n        std::cout &lt;&lt; \"Caught unknown exception.\" &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><br>In this example, if the user enters a negative age, the MyException object will be thrown, and it will be caught by the corresponding catch block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-standard-exceptions\"><strong>C++ Standard Exceptions<\/strong><\/h2>\n\n\n\n<p>In C++, you can create your own user-defined exceptions to handle specific error conditions in your code. User-defined exceptions allow you to define custom exception types that inherit from the standard C++ <code>std::exception<\/code> class or any of its derived classes. This enables you to throw and catch specific exception types that represent different error situations.<\/p>\n\n\n\n<p>Here's a step-by-step guide on how to define and use user-defined exceptions in C++:<\/p>\n\n\n\n<p>Step 1: Define your custom exception class<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;exception&gt;\n#include &lt;string&gt;\n\nclass MyException : public std::exception {\npublic:\n    MyException(const std::string&amp; message) : message_(message) {}\n\n    \/\/ Override the what() method to provide error description\n    const char* what() const noexcept override {\n        return message_.c_str();\n    }\n\nprivate:\n    std::string message_;\n};\n<\/pre><\/div>\n\n\n<p>Step 2: Throw the user-defined exception<br>You can throw the custom exception in your code when a specific error condition is encountered. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n\ndouble divideNumbers(double numerator, double denominator) {\n    if (denominator == 0) {\n        throw MyException(&quot;Division by zero is not allowed.&quot;);\n    }\n    return numerator \/ denominator;\n}\n\nint main() {\n    try {\n        double result = divideNumbers(10.0, 0.0);\n        std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; result &lt;&lt; std::endl;\n    } catch (const MyException&amp; ex) {\n        std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; ex.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>In this example, we defined a custom <code>MyException<\/code> class and used it to throw an exception when dividing by zero in the <code>divideNumbers<\/code> function. In the <code>main<\/code> function, we catch the exception and handle it by printing the error message.<\/p>\n\n\n\n<p>Step 3: Handle the user-defined exception<br>When an exception is thrown, you can catch it using a <code>try<\/code> block and handle it with a corresponding <code>catch<\/code> block. In this example, we catch <code>MyException<\/code> and print its error message using the <code>what()<\/code> method.<\/p>\n\n\n\n<p>User-defined exceptions are helpful for providing meaningful error messages and handling specific error scenarios in your code. You can create multiple custom exception classes to represent different types of errors, which allows for better organization and readability in your exception handling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-c-standard-exceptions\">What is C++ Standard Exceptions?<\/h3>\n\n\n\n<p>C++ standard exceptions provide a list of standard exceptions defined in &lt;exception&gt; which we can use in our programs. <br><strong>These exceptions are arranged in a parent-child class hierarchy:<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-defined-exceptions\"><strong>User-Defined Exceptions&nbsp;<\/strong><\/h2>\n\n\n\n<p>In C++, you can create your own user-defined exceptions to handle specific error conditions in your code. User-defined exceptions allow you to define custom exception types that inherit from the standard C++ <code>std::exception<\/code> class or any of its derived classes. This enables you to throw and catch specific exception types that represent different error situations.<\/p>\n\n\n\n<p>Here's a step-by-step guide on how to define and use user-defined exceptions in C++:<\/p>\n\n\n\n<p>Step 1: Define your custom exception class<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;exception&gt;\n#include &amp;lt;string&gt;\n\nclass MyException : public std::exception {\npublic:\n    MyException(const std::string&amp;amp; message) : message_(message) {}\n\n    \/\/ Override the what() method to provide error description\n    const char* what() const noexcept override {\n        return message_.c_str();\n    }\n\nprivate:\n    std::string message_;\n};\n<\/pre><\/div>\n\n\n<p>Step 2: Throw the user-defined exception<br>You can throw the custom exception in your code when encountering a specific error condition. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n\ndouble divideNumbers(double numerator, double denominator) {\n    if (denominator == 0) {\n        throw MyException(&quot;Division by zero is not allowed.&quot;);\n    }\n    return numerator \/ denominator;\n}\n\nint main() {\n    try {\n        double result = divideNumbers(10.0, 0.0);\n        std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; result &lt;&lt; std::endl;\n    } catch (const MyException&amp; ex) {\n        std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; ex.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p>In this example, we defined a custom <code>MyException<\/code> class and used it to throw an exception when dividing by zero in the <code>divideNumbers<\/code> function. In the <code>main<\/code> function, we catch the exception and handle it by printing the error message.<\/p>\n\n\n\n<p>Step 3: Handle the user-defined exception<br>When an exception is thrown, you can catch it using a <code>try<\/code> block and handle it with a corresponding <code>catch<\/code> block. In this example, we catch <code>MyException<\/code> and print its error message using the <code>what()<\/code> method.<\/p>\n\n\n\n<p>User-defined exceptions are helpful for providing meaningful error messages and handling specific error scenarios in your code. You can create multiple custom exception classes to represent different types of errors, which allows for better organization and readability in your exception handling.<\/p>\n\n\n\n<p>This brings us to the end of the blog on Exception Handling in C++. Hope this helps you to up-skill your C++ skills. To learn more about programming and other related concepts, check out the courses on&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/academy\/#our-courses\" target=\"_blank\">Great Learning Academy<\/a>.&nbsp;<\/p>\n\n\n\n<p>Also, if you are preparing for Interviews, check out these&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Interview Questions for C++<\/a>&nbsp;to ace it like a pro<\/p>\n\n\n\n<p>Seize the opportunities that await you through our dynamic range of <a href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\" rel=\"noreferrer noopener\">free courses<\/a>. Whether you're interested in Cybersecurity, Management, Cloud Computing, IT, or Software, we offer a broad spectrum of industry-specific domains. Gain the essential skills and expertise to thrive in your chosen field and unleash your full potential.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exception handling in C++ is a particular condition for developers to handle. In programming, committing mistakes that prompt unusual conditions called errors is normal. All in all, these errors are of three kinds: What is Exception Handling in C++?&nbsp; Exception handling in C++ is a mechanism that allows a program to deal with runtime errors [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":25680,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-25670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Exception Handling in C++ | What is Exception Handling in C++<\/title>\n<meta name=\"description\" content=\"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exception handling in C++ | What is Exception handling in C++\" \/>\n<meta property=\"og:description\" content=\"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-17T06:52:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:38:21+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Exception handling in C++ | What is Exception handling in C++\",\"datePublished\":\"2023-07-17T06:52:41+00:00\",\"dateModified\":\"2024-09-03T05:38:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/\"},\"wordCount\":2235,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/shutterstock_463433567-1.jpg\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/\",\"name\":\"Exception Handling in C++ | What is Exception Handling in C++\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/shutterstock_463433567-1.jpg\",\"datePublished\":\"2023-07-17T06:52:41+00:00\",\"dateModified\":\"2024-09-03T05:38:21+00:00\",\"description\":\"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/shutterstock_463433567-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/shutterstock_463433567-1.jpg\",\"width\":1000,\"height\":667,\"caption\":\"Exception Handling in C++\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-cpp\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Exception handling in C++ | What is Exception handling in C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Exception Handling in C++ | What is Exception Handling in C++","description":"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Exception handling in C++ | What is Exception handling in C++","og_description":"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions","og_url":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-07-17T06:52:41+00:00","article_modified_time":"2024-09-03T05:38:21+00:00","og_image":[{"width":1000,"height":667,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Exception handling in C++ | What is Exception handling in C++","datePublished":"2023-07-17T06:52:41+00:00","dateModified":"2024-09-03T05:38:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/"},"wordCount":2235,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/","url":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/","name":"Exception Handling in C++ | What is Exception Handling in C++","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg","datePublished":"2023-07-17T06:52:41+00:00","dateModified":"2024-09-03T05:38:21+00:00","description":"Exception Handling in C++ is defined as a method that takes care of a surprising condition like runtime errors. They use Try, Catch and Throw conditions","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg","width":1000,"height":667,"caption":"Exception Handling in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Exception handling in C++ | What is Exception handling in C++"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",1000,667,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",1000,667,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",1000,667,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",1000,667,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",640,427,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",96,64,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/shutterstock_463433567-1.jpg",150,100,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Exception handling in C++ is a particular condition for developers to handle. In programming, committing mistakes that prompt unusual conditions called errors is normal. All in all, these errors are of three kinds: What is Exception Handling in C++?&nbsp; Exception handling in C++ is a mechanism that allows a program to deal with runtime errors&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=25670"}],"version-history":[{"count":38,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25670\/revisions"}],"predecessor-version":[{"id":106675,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25670\/revisions\/106675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/25680"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=25670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=25670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=25670"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=25670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}