Exception handling concept.
- Exceptions are certain disastrous error conditions that occur during the execution of a program. They could be errors that cause the programs to fail or certain conditions that lead to errors. If these run time errors are not handled by the program, OS handles them and program terminates abruptly, which is not good.
To avoid this, C++ provides Exception Handling mechanism.
- The three keywords for Exception Handling are: Try, Catch and Throw.
- The program tries to do something. If it encounters some problem, it throws an exception to another program block which catches the exception.
-
Example :void main()
{
int no1, no2;
try
{
cout << “Enter two nos:”;
cin >> no1 >> no2;
if (no2 == 0)
throw “Divide by zero”;
else
throw no1/no2;
}
catch (char *s)
{
cout << s;
}
catch (int ans)
{
cout << ans;
}
}
- We know that divide by zero is an exception. If user enters second no as zero, the program throws an exception, which is caught and an error message is printed else the answer is printed.
Describe Exception handling for a class with an example.
- Exception handling allows the programmer to manage runtime errors in an orderly fashion. Using exception handling, the program can automatically invoke an error handling routine when an error occurs.
- An exception which is of class type is known as Class Type Exception. The reason to define a class type for an expression is to create an object that describes the error that occurred. This information can be used by the exception handler to process the error. In general, exception classes are created to encapsulate information about an error to enable the exception handler to respond effectively.
- Consider the following sample code :
Class MyException
{
public:
char str_exc[80];
int exc;
MyException()
{
*str_exc = 0;
exc = 0;
}
MyException(char*s, int e)
{
strcpy(str_exc, s);
exc = e;
}
};
int main()
{
int no;
try
{
cout << “Enter positive no:”;
cin >> no;
if (no < 0)
throw MyException(“Not Positive”, no);
}
catch (MyExecption e)
{
cout << e.str_exc<<”: “<<e.exc <<”\n”;
}
return 0;
}
Sample run :Enter positive No: -2
Output: Not positive: -2
- Thus, the program prompts the user for a positive no. If negative no is entered, an object of the class MyException is created which encapsulates the information about the error.
Describe exceptions in C++.
Exceptions :- Exceptions are certain disastrous error conditions that occur during the execution of a program. They could be errors that cause the programs to fail or certain conditions that lead to errors. If these run time errors are not handled by the program, OS handles them and program terminates abruptly, which is not good. So C++ provides Exception Handling mechanism.
- Exceptions are of two types:
1. Synchronous Exceptions : Errors that occur due to incorrect input data or incorrect handling of array indices (“out of range index”), memory overflow are known as Synchronous Exceptions.
2. Asynchronous Exceptions : The errors that are caused by events that are beyond control of the program are Asynchronous Exceptions. E.g. Keyboard interrupts.
- C++ exception handling mechanism takes care of only Synchronous Exceptions.
- The benefits of Exception Handling are:
1. Program is not terminated abruptly
2. User will understand what errors are occurring in the program.
- The three keywords for Exception Handling are: Try, Catch and Throw.
- The program tries to do something. If it encounters some problem, it throws an exception to another program block which catches the exception.
- Consider the following program code:
void main()
{
int no1, no2;
try
{
cout << “Enter two nos:”;
cin >> no1 >> no2;
if (no2 == 0)
throw “Divide by zero”;
else
throw no1/no2;
}
catch (char *s)
{
cout << s;
}
catch (int ans)
{
cout << ans;
}
}
- We know that divide by zero is an exception. If user enters second no as zero, the program throws an exception, which is caught and an error message is printed.
- Please note that catch is not a function; it is a program block.