To catch the exceptions, you place some section of code under exception inspection. The section of code is placed within the try-catch block. In case no exceptional circumstance occurs, the code will execute normally. The handlers will be ignored. In this C++ tutorial, you will learn:
What is Exception Handling in C++?
Why Exception Handling?
Exception Handling Keywords
Syntax:
C++ Standard Exceptions
User-Defined Exceptions
Why Exception Handling?
Here, are the reason for using Exception Handling in C++:
You will separate your error handling code from your normal code. The code will be more readable and easier to maintain. Functions can handle the exceptions they choose. Even if a function throws many exceptions, it will only handle some. The caller will handle the uncaught exceptions.
Exception Handling Keywords
Exception handling in C++ revolves around these three keywords:
throw– when a program encounters a problem, it throws an exception. The throw keyword helps the program perform the throw. catch– a program uses an exception handler to catch an exception. It is added to the section of a program where you need to handle the problem. It’s done using the catch keyword. try– the try block identifies the code block for which certain exceptions will be activated. It should be followed by one/more catch blocks.
Suppose a code block will raise an exception. The exception will be caught by a method using try and catch keywords. The try/catch block should surround code that may throw an exception. Such code is known as protected code.
Syntax:
The try/catch takes this syntax:
Although we have one try statement, we can have many catch statements. The ExceptionName is the name of the exception to be caught. The exception1, exception2, and exceptionN are your defined names for referring to the exceptions.
Example 1:
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file in the program to use its functions. Include the vector header file in the program to use its functions. Include the std namespace in the program to its classes without calling it. Call the main() function. The program logic should be added within its body. Create a vector named vec to store integer data. Add the element 0 to the vector named vec. Add the element 1 to the vector named vec. A comment. It will be skipped by the C++ compiler. Use the try statement to catch an exception. The { marks the beginning of the body of try/catch block. The code added within the body will become the protected code. Try to access the element stored at index 2 (third element) of the vector named vec. This element doesn’t exist. End of the body of try/catch block. Catch the exception. The returned error message will be stored in the variable ex. Print out some message on the console if the exception is caught. End of the body of the catch block. The program should return a value upon successful execution. End of the main() function body.
Example 2:
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file in the program to use its functions. Include the std namespace in the program to its classes without calling it. Create a function named zeroDivision that takes two integer arguments, x, and y. The function should return a double result. Use an if statement to check whether the value of variable argument y is 0. The { marks the beginning of if body. The message to be returned/thrown if y is 0. End of the body of the if statement. The zeroDivision function should return the value of x/y. End of the body of the zeroDivision function. Call the main() method. The { marks the beginning of this method. Declare an integer variable and assigning it the value 11. Declare an integer variable b and assigning it the value 0. Declare a double variable c and assigning it the value 0. Use the try statement to catch an exception. The { marks the beginning of the body of try/catch block. The code added within the body will become the protected code. Call the zeroDivision function and passing to arguments a and b, that is, 11 and 0. The result of this operation will be stored in variable c. Print out the value of variable c on the console. End of the body of try/catch block. Catch the exception. The returned error message will be stored in the variable message. Print out the returned error message on the console. End of the body of the catch block. The program should return a value upon successful execution. End of the main() function body.
C++ Standard Exceptions
C++ comes with a list of standard exceptions defined in
User-Defined Exceptions
The C++ std::exception class allows us to define objects that can be thrown as exceptions. This class has been defined in the
Example:
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file in our program. We will use its functions without getting errors. Include the exception header file in our program. We will use its functions like what without errors. Include the std namespace in our program to use its classes without calling it. Create a new class named newException. This class inherits the exception class of C++. The beginning of the class body. Overwrite the virtual member function what() defined in the exception header file. We will then describe our own exception, the new exception. Start the definition of the new exception. The message to be returned if the new exception is caught. End of the definition of the new exception. End of the body of class newException. The newex is the name to be used to catch our new exception, after which the newException will be called. Call the main() function. The program logic should be added within its body. The { marks the beginning of its body. Use a try statement to mark the code within which we need to mark the exception. The { marks the beginning of the body of try/catch block. The code surrounded by this will become protected. Throw the newex exception if it’s caught. End of the try body. Use the catch statement to catch the exception. The exception error message will be stored in variable ex. Print the exception error message on the console. End of the body of catch statement. The program should return a value if it executes successfully. End of the body of the main() function.
Summary:
With exception handling in C++, you can handle runtime errors. Runtime errors are the errors that occur during program execution. Exception handling helps you handle any unexpected circumstances in your program. When the unexpected circumstance occurs, program control is transferred to handlers. To catch an exception, you place a section of code under the try-catch block. The throw keyword helps the program throw exceptions, helping the program to handle the problem. The try keyword helps identify the code block for which certain exceptions will be activated. We can overwrite the what() function of the exception header file to define our exceptions.