The concept of exception handling is very simple. The basic idea is simply to raise
an error flag when something goes wrong.
A D V E R T I S E M E N T
Next, there is a system which always
lookout for these error flag. finally, previous system calls an error handling code
if a error flag is spotted.
Program Flow
Raising an imaginary error flag is called throwing up an error.
When the error is been thrown overall system will respond by catching an error.
Surrounding the block of the error-sensitive code with an exception handling is
called trying to execute the block of code.
The most powerful features of the exception handling is, an error can be thrown
over the function boundaries. Means that if one of a deepest functions on a stack
has error, this can be propagate to a upper function if there is the
trying-block of a code there. This will allow the programmers to put an error handling
code in a place, like the main-function of the program.
C++ exception handling
Designers of the C++ programming language, extended the language with the exception
handling structures. Commands which are used relate closely to the terms used in the
exception handling (as described above). Block of code which you want to try out starts
by specifying a "try" command and surrounding a block with the curly braces. Inside this
block, you are allowed to throw the occurring errors with a "throw" command. You should
specify the error and this must be a class. After a try-block is been closed,
a catch-block will start. This is illustrated in the code below.
Here the exception is the defined class with the constructor with no parameters been
passed. It will be useful to have the info on what type of error has occurred. This
can be done by two methods. Either can define the different exception-classes and then
throw them according to the error been occurred. Or can give
a class parameter containing error message and allow a class to display the
message.
Exception Handling System
A class which we are about to design should have to store the information about an error
which occurred and a class should be able to display the error message. Following code
shows this functionality.
class CException
{
public:
char* message;
CException( char* m )
{
message = m
};
Report();
}