• The unusual conditions such as array out of bound, overflows, input output errors etc causes the program to fail.
• When an abnormal situation arises at runtime, the program either should terminate or should take some necessary measures before the program termination. There are two types of exceptions
– Synchronous Exception: occur due to the problem in input data or inappropriate way of handling the data.
– Asynchronous exception: occur due to hardware failure and beyond the control of the program.
• C++ Programs only handle the synchronous type of exceptions.
• The necessary measures that is taken to handle the abnormal situation at runtime is called exception handling and it is done by some handler function.
• When the program encounters an abnormal situation, the program control can be transferred to other block of the program that is designed to handle the situation called handler. It can be transferred by throwing an exception. Then the handler catches the exception thrown and handles the situation appropriately.
• In C++, exception handling mechanism is done by using the keywords try, throw and catch.
• The part of the code that can generate the exception should be placed within the try block and the part of the code to handle appropriate exception should be placed within the catch block.
• When the exceptional circumstances arises within that block, the exception is thrown to the handler.
• If no exception is thrown from the try block, the code continues normally and all handlers are ignored.
• The exception handler is declared with keyword catch immediately after the try block.
Syntax
Example
Output: An exception occurred: exception no:20• The code under exception handling is enclosed in a try block. In previous example this code simply throws an exception
– throw 20;
• A throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to the exception handler.
• The exception handler is declared with the catch keyword. As you can see, it follows immediately the closing brace of the try block.
• The catch format is similar to a regular function that always has at least one parameter.
• The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught.
• We can chain multiple handlers(catch expressions), each one with a different parameter type.
• Only the handler that matches its type with the argument specified in the throw statement is executed.
• If we use an ellipsis (...) as the parameter of catch, that handler will catch any exception no matter what the type of the throw exception is.
• This can be used as a default handler that catches all exceptions not caught by other handlers if it is specified at last:
• In this case the last handler would catch any exception thrown with any parameter that is neither an int nor a char.
The try Block
• The block of code where there is chance of raising an exception is enclosed within this block.• The try block is responsible for testing the existence of exception.
• If an exception occurs , it should throw the exception to the catch block which is the handler function.
Syntax:
try
{
// code that may raise the exception
// call function to handle exception by throw
}
throw construct
• When a problem is detected during execution, an exception is raised by using keyword throw.• The throw exception is inside the try block and uses one parameter to throw e. g. an integer 20 in previous example, which is passed as an argument to the exception handler function catch()
try
{
//--------------------
If( error)
throw param;
}
catch block
• The raised exceptions are handled by the catch block. The catch construct must be used immediately after the try block.• When using multiple catch, other catch block must follow the previous catch.
• Each handler evaluates an exception that matches the parameter specified in the list. The syntax is
catch( type arg)
{
// handling code
}
Steps taken during exception handling
• Hit the exception i.e. detect a problem or error in try block• Throw the exception i.e inform the error
• Catch the exception i.e. receive the error information
• Handle the exception i.e. take appropriate action.
Example
Using exception class
• We need to create a new kind of entity called exception class to handle the exception in the class.• The syntax is
class Aclass
{
public:
class AnError {}; // exception class
void fun()
{
if(error condition)
throw AnError(); // throw exception
}
};
int main()
{
try
{
Aclass obj1;
Obj1.fun(); // may cause error
}
catch(Aclass::AnError)
{
//tell user about error
}
}
Example: Exception Class
OutputOutput with positive input:
Enter num:45
Trying to find squre root.....
Square root is :6.7082
Success: No exception raised
Output with negative input
Enter num:-45.5
Trying to find squre root.....
Square root of Negative no not possible.
0 Comments