Exception act as an event that takes place while executing any program, which throws the normal flow of instructions of the program.
Suppose we are writing one program where we forget to declare one variable and trying to use that variable. Our system show error that undefined symbol, this is possible by the predefined program. whenever this error occurs, it automatically displays this error on the screen. Now the user is easy to understand what is the error and also able to do the correction. This same thing is provided for C++ compiler through exception handling.
The conversion of system error message into the user-friendly message is known as exception handling.
Generally, errors are of three types compile, logical and execution time errors.
Compile time errors can be syntax, library references, Logical errors can be due to the wrong logic is written on the program. Runtime errors are also called as exception errors. Error handling can be done. Exception handling take place during runtime error.
When anyone tries to divide no of 0 i.e. y/0. It compiles successfully but generate runtime error.
Advantage of Exception Handling
It permits users to handle runtime errors and maintain the normal flow of C++ programming language application.
Types of Exception
These are generated due to some unusual conditions such as Array index out of bound,
These exceptions are due to such events that are out of C++ program. Such as keyword
C++ exception handling mechanism is generated for handling synchronous exceptions. The main objective of the handling exception concept is to make available means for detecting and reporting an exception such that convenient action can be performed. For this, a separate error handling code is required that operate the following tasks:
a) Search and hit exceptions
b) Inform that the error has occurred (throw exception)
c) Catch exceptions in which error information can be received.
d) Handle exceptions through corrective actions
This block includes two blocks
Keywords used in Exception Handling
In C++ programming language, there are following three keywords used for operating Exception handling
In C++ programming language, an exception can be thrown by the throw keyword. When some possibilities of some exceptional condition are known in the program, the exception throw. The throw expression is used for error related communication where one parameter is accepted by throw expression and then, it is then handover to the handler.
The main usage of the catch block is to catch the program's error and manage the exception condition. There can be multiple catch blocks in the C++ program.
The main usages of the Try block to throw exceptions that can further proceed through catch blocks. There can be only one try block.
Example of exception in C++ programming language
This following two program compiles but display runtimes error
using namespace std;
Before applying try
Inside the block of try
Exception in the program Caught
After the catch block
Following are some standard exceptions found in C++ that can be used under <exception> header.
Std::exception and many more