![]() |
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 ExceptionC++
Output
Caught an exception: This is a custom exception Related Articles: |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |