C++ Throwing and Catch Exception
When an exception that is desired to be handled is detected , it is thrown using the throw statement(throw keyword) in one of the following forms show below
throw(exception);
throw exception;
throw;
|
The operand object exception may be of any type, including constant, it is also possible to throw objects not intended for error handling
Whenever an exception is thrown, it will be caught by the catch statement(block) associated with the try block. that is the control exist the current try block, and is transferred
to the catch block after that try block
Throw point can be in a deeply nested scope within a try block or in a deeply nested function call. in any one case the control is transferred to the catch statement
CATCHING MECHANISM
As stated earlier, code for handling exception is included in catch blocks. A catch block looks like any function definition who catch the exception and is of the form
The type indicates the type of the exception that catch block handles. The argument arg is an optional parameter name.
Note : the exception handling code is placed between the two braces.
The catch statement or block catches an expression whose type matches with the type of catch argument. When it is caught, the code inside the catch block is executed
If the parameter in the catch statement is named, then the parameter can be use in the code of Exception handling. After execution the handler, the control goes to The statement immediately following the catch block
due to mismatch, if an exception is not caught, abnormal program termination will occur. It is important to
note that the catch block is simply skipped if the catch statement does not catch an exception