Home >Backend Development >C++ >C++ program creates custom exception
Exception is a very core concept of C. Exceptions occur when an undesired or impossible operation occurs during execution. Handling these unwanted or impossible operations in C is called exception handling. Exception handling mainly uses three specific keywords, which are ‘try’, ‘catch’ and ‘throw’. The ‘try’ keyword is used to execute code that may encounter exceptions, the ‘catch’ keyword is used to handle these exceptions, and the ‘throws’ keyword is used to create exceptions. Exceptions in C can be divided into two types, namely STL exceptions and user-defined exceptions. In this article, we focus on how to create these custom exceptions. More details on exception handling can be found here.
First, we saw how to create custom exceptions using a single class. To do this we have to define a class and throw an exception from that class.
//user-defined class class Test{}; try{ //throw object of that class throw Test(); } catch(Test t) { .... }
#include <iostream> using namespace std; //define a class class Test{}; int main() { try{ //throw object of that class throw Test(); } catch(Test t) { cout << "Caught exception 'Test'!" << endl; } return 0; }
Caught exception 'Test'!
The 'try' block will throw exceptions of that class, while the 'catch' block will only catch exceptions of that specific class. If there are two user-defined exception classes, they must be handled separately.
The process is simple, as expected, if there are multiple exceptions, each must be handled individually.
//user-defined class class Test1{}; class Test2{}; try{ //throw object of the first class throw Test1(); } catch(Test1 t){ .... } try{ //throw object of the second class throw Test2(); } catch(Test2 t){ .... }
#include <iostream> using namespace std; //define multiple classes class Test1{}; class Test2{}; int main() { try{ //throw objects of multiple classes throw Test1(); } catch(Test1 t) { cout << "Caught exception 'Test1'!" << endl; } try{ throw Test2(); } catch(Test2 t) { cout << "Caught exception 'Test2'!" << endl; } return 0; }
Caught exception 'Test1'! Caught exception 'Test2'!
We had to use two different try-catch blocks to handle two different categories of exceptions. Now let's see if we can create and handle exceptions using constructors.
We can use the class constructor to create custom exceptions. In the example below, we can see that exception throwing and handling are managed inside the class constructor.
#include <iostream> using namespace std; //define a class class Test1{ string str; public: //try-catch in the constructor Test1(string str){ try{ if (str == "Hello"){ throw "Exception! String cannot be 'Hello'!"; } this->str = str; } catch(const char* s) { cout << s << endl; } } }; int main() { Test1 t("Hello"); return 0; }
Exception! String cannot be 'Hello'!
Exception handling is one of the most important functions provided by C. We can inherit the C exception class and use it to implement exception handling, but this is just good practice and not required to create custom exceptions. The benefit of inheriting the C exception class is that if there is a normal catch statement catching std::exception, it can handle any user-defined exception without needing to know the specific details. It should be noted that the 'throws' statement only works within the 'try' block, otherwise it will not work. The 'catch' statement can only handle exceptions thrown by user-defined classes or certain STL classes.
The above is the detailed content of C++ program creates custom exception. For more information, please follow other related articles on the PHP Chinese website!