How to Throw a Custom Exception in C++? Last Updated : 02 Jun, 2025 Comments Improve Suggest changes Like Article Like Report 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 exception in C++.Throwing Custom Exceptions in C++To throw a custom exception, we first have to create a custom exception class. This class inherits the std::exception class from <exception> header. We override the what() method of this class to provide a custom error message.The last step is to use this user-defined custom exception in our code. The below method demonstrates how to do it.C++ Program to Throw a Custom Exception C++ #include <bits/stdc++.h> using namespace std; // Define a new exception class that // inherits from std::exception class MyException : public exception { private: string message; public: // Constructor accepting const char* MyException(const char* msg) : message(msg) {} // Override what() method, marked // noexcept for modern C++ const char* what() const noexcept { return message.c_str(); } }; int main() { try { // Throw custom exception with // const char* throw MyException("This is a custom exception"); } // Catch by const reference (good practice) catch (const MyException& e) { printf("Caught an exception: %s\n", e.what()); } return 0; } OutputCaught an exception: This is a custom exception Related Articles:User-defined Custom Exception with class in C++Exception Handling in C++ Comment More infoAdvertise with us Next Article How to Throw a Custom Exception in C++? S susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ cpp-exception Exception Handling C++-Exception Handling CPP Examples +2 More Practice Tags : CPP 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 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 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