Exception Handling
This blog has been migrated to learn c programming for beginners
Exception Handling is the mechanism that separate the code that detects and handle exceptions at run time from rest of program
C++ exception handling is built upon three keywords: try, catch, and throw.
Exception Handling vs Traditional Error Handling
Following are main advantages of exception handling over traditional error handling.
1) Separation of Error Handling code from Normal Code:
In traditional error handling codes, there are always if else conditions to handle errors. These conditions and the code to handle errors get mixed up with the normal flow. This makes the code less readable and maintainable. With try catch blocks, the code for error handling becomes separate from the normal flow.
2) Functions/Methods can handle any exceptions they choose:
A function can throw many exceptions, but may choose to handle some of them. The other exceptions which are thrown, but not caught can be handled by caller. If the caller chooses not to catch them, then the exceptions are handled by caller of the caller.
In C++, a function can specify the exceptions that it throws using the throw keyword. The caller of this function must handle the exception in some way (either by specifying it again or catching it)
In C++, a function can specify the exceptions that it throws using the throw keyword. The caller of this function must handle the exception in some way (either by specifying it again or catching it)
3) Grouping of Error Types:
In C++, both basic types and objects can be thrown as exception. We can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.
Exception Handling Model
The error handling mechanism perform following task :
- Find the problem (Hit the Exception)
- Inform that error has occured (Throw the exception)
- Receive the error information (Catch the exception)
- Take corrective ations (Handle the exception)
Try Block
A try/catch block is placed around the code for which particular exception will be analysed and detected.It is followed by one or more catch blocks . The try block doesn't define how we are handling an exception but it merely tells the program , "if you see an exception in the following code , grab it". If the try block detects exception , the exception is thrown to catch block using throw keyword and program control also transfer to catch block , it never returns to try blocks again. If no error is found within try block , catch block is escaped .
Syntax:try
{
//code to be monitered.
}
Throw Block
An exception is thrown in a particular catch block using throw keyword. It can throw object of different types , known as exception objects.
When try blocks is executed , whenever exceptions are found they are thrown through throw blocks and the program leaves the try block and enters into the catch blocks . If type of object thrown in try block match the argument type in the catch statement ,
the catch block are executed to handle those exception . If no match catch are found , then program is aborted with the help of abort() function which is invoked by default
the catch block are executed to handle those exception . If no match catch are found , then program is aborted with the help of abort() function which is invoked by default
Catch Block
The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.
Syntaxcatch ()
{
//block of statement that handles the exception
}
Rethrowing an Exception
If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression (throw without assignment_expression) causes the originally thrown object to be rethrown.
Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the dynamically enclosing try block have an opportunity to catch the exception.
Example
This program implement Rethrowing an Exception
Output
Caught Exception
ERROR
Comments
Post a Comment