Learn What is Exception Handling in 2 Minutes
Exception Handling
Writing error-free programs is the main objective of
programmers. However, whenever
a program is written for the first time, it usually contains
errors. These errors are classified
mainly into three
types, namely, compile-time errors, logical errors and run time errors.
The compile-time errors and logical errors can be detected
and removed by exhaustive
testing and debugging. However, run-time errors are
difficult to detect and trap. The run-time
errors are usually referred to as exceptions.
An exception is an unexpected event that occurs during the
execution of a program and
alters the normal flow of the program or terminates the
program abnormally. The errors
that can cause exceptions can range from simple programming
errors, such as opening an
invalid file for reading, invalid array index, etc., to
serious errors such as running out of memory.
Exceptions can be classified into two types, namely,
synchronous exceptions and asynchronous
exceptions. The exceptions that occur at specific program
statements are called synchronous
exceptions. For example, deleting from an empty list,
invalid array index, stack overflow, etc., are
synchronous exceptions.
On the other hand, the exceptions that are issued by
external sources which are beyond the control
of the program are called asynchronous exceptions. For
example, the hardware interrupts such as
pressing Ctrl-C to interrupt a program is an asynchronous
exception.
C++ provides an efficient exception handling mechanism to
handle synchronous exceptions. This chapter
begins with an overview of the conventional error-handling
mechanism and then discusses the C++
exception handling mechanism in detail.
Conventional
Error-handling Mechanism
In the conventional method of handling run-time error, the
error codes are returned
from the function in which the error has occurred and
are passed to the calling function.
The calling function checks for the return values and then
appropriately handles the error.
To understand this concept, consider this
example.
Example :
A program to demonstrate the conventional
error-handling mechanism
#include<fstream>
#include<stdlib>
using namespace std:
int err_func(char a[])
{
ifstream ifile
char *file1:
cout<<"Enter the name
of the file;
cin>>file1;
ifile.open(file1);
if (ifile==NULL)
return 1:
else
{
ifile.get (a,
80);
ifile.close();
ifile(a[0]==
'10')
return 2;
}
} //end of err_func()
int main()
{
char a [10];
strcpy (a,
“*”);
int ret=err_func(a);
if (ret==1)
{
cout<<”File
does not exist”;
exit(0);
}
cout<<” File opened”;
if(ret==2)
cout<<”\nFile
is empty”;
else
cout<<”\nThe
contents of file are:<<a;
return 0;
} //End
of Program
The output of the program is
First Run
Enter the name of the file:
test.txt
Flle does not exist
Second run
Enter the name of the file:
test.txt
File opened
The contents of file are: Hello
Third Run
File opened
Enter the name of the file:
tentl.txt
File is empty
In this example, a function err_func() is defined to handle
run-time errors that can occur while opening and reading a
file. The function
returns integer values that are passed to the calling function main(), which
handles these errors by
displaying appropriate messages. However, this method
has few limitations.
The function that checks the errors has to be called
explicitly. Thus, it is difficult to handle the errors that occur in a class
constructor as the constructor is called implicitly. Hence, this method is not
suitable for classes. In other words, this
method does not guarantee that run-time
errors are actually caught and handled.
It does not provide a way to separate the error handling
code from the code written to handle the other tasks of the
program. This makes
it difficult to read and write the code.
If you want the source code of this example then ask it out
in the comments section I will post it there :)…
Comments
Post a Comment