Hello friends, in this post today, we learn about What is exception handling in c++ and how to handle exception in c++. so lets start...
Exception handling |
Exception handling:-
Exception is any abnormal behaviour.
In C++ errors can be two types.
1. Compile time errors and
2. Run time errors.
Compile time errors are syntax errors which occurs during the writing of the program. Most common examples of compile time errors are missing semicolon, missing comma, missing double quotes, etc.
The syntax error is detected during compilation of program, but the Run time error will detect during execution of program. So, it is very difficult to handle logical error.
C++ provides exception handling mechanism which can be used to trap this exception and running programs smoothly after catching the exception.
The exception handling provides mechanism to handle logical error during execution of program with appropriate response.
Steps to handle Run time error:
1. Find the problem (Hit the exception) and write an exception in try block.
2. Inform that an error has occurred (Throw the exception)
3. Receive the error information (Catch the exception)
4. Take corrective actions (Handle the exception)
Exception Handling Mechanism:
This mechanism is built upon three keyword: try, throw and catch.
Try block
try is used to preface a block of statement which may generate exceptions. Whenever an exception is generated in the try block, it is thrown .An exception is an object so we can say that an exception object is thrown. The throw keyword is used for throwing an exception. A try block must have at least one catch block.
Throw
Throw must be executed either within the try block proper or from any function that the code within the block calls.
catch block
A catch block is used to catches the exceptions thrown by throw statement and take appropriate action. catch block for catching different types of exceptions. A catch block must have a try block prior written which will throw an exception. If exception thrown matches with the argument or object in the block, the statements written within the catch was caught successfully by the catch block .After the successful execution of the catch block statements any statements following the catch block will be executed. The relationship between try, throw and catch block is as shown in below figure.
Exception handling |
Syntax:
Example:
Output:
When the value of q is zero at that time exception will throw and this exception will catch in catch block and print the message value is zero.
Multiple catch statements:-
It is possible that a program segment has more than one condition to throw an exception. For these situations we can put multiple catch blocks for a single try in case try block throws different types of exceptions.
When an exception is thrown, the exception handlers are searched in order for an appropriate match. The fist handler that yields a match is executed.
After executing the handler, the control goes to the first statement after the lat catch block for that try.
Note: It is possible that arguments of several catch statements match the type of an exception.
In such cases, the first handler that matches the exception type is executed.
Example:
Output:-
Re-throwing an Exception:-
A handler may decide to rethrow the exception caught without processing it.
In such situations, we may simply invoke throw without any arguments as shown below:
throw;
This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
Example:
Output:-
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment