One, a simple example
First, familiarize yourself with the try/catch/throw of C + + by using a simple example:
1 #include <iostream.h>//Include header file
2 #include <stdlib.h>
3 double fuc (double x, double y)//define function
4 {
5 if (y==0)
6 {
7 Throw y; Divisor is 0 and throws an exception
8}
9 return x/y; Otherwise return two number of quotient
10}
one void Main ()
12 {
Double res;
Try//Definition exception
15 {
RES=FUC (2,3);
cout<< "The result of x/y is:" <<res<<endl;
RES=FUC (4,0); An exception occurred
19}
catch (double)//catch and handle exceptions
21 {
cerr<< "error of dividing zero.\n";
To exit (1); Exception Exit Program
24}
25}
The data type of the catch needs to match the throw data type.
second, catch (...) ) The Role
catch (...) Can capture the exception object of a variety of data types, so it provides programmers with a better control of the exception object, so that the development of the software system has good reliability. So a more experienced programmer would normally organize the code modules for it, as follows:
void Func ()
{
Try
{
The program code here completes the really complex computations that, during execution,
It is possible to throw exception objects of the DataType1, DataType2, and DataType3 types.
}
catch (datatype1& D1)
{
}
catch (datatype2& D2)
{
}
catch (datatype3& D3)
{
}
Note The DataType1, DataType2, and DataType3 three that may be thrown in the try block above
A type of exception object already has a corresponding catch block in front of it. But why
Also define a catch (...) block at the end. This is to have better security and
Reliability to avoid the process that occurs when the above try block throws out other exception objects that are not considered
Sequence has the serious consequences of an accidental crash, and this is more particularly effective in systems developed with VC
For catch (...) Can capture the system of the exception, and system abnormalities often make programmers headache, now
In the system is generally more complex, and by a lot of people jointly developed, inadvertently will lead to a
The pointer variable points to other illegal areas, resulting in an unexpected disaster. catch (...) For this
Potential pitfalls provide an effective remedy.
catch (...)
{
}
}
Iii. Object-oriented processing in the anomaly
First look at the following example:
void OpenFile (string f)
{
Try
{
Open file operation, may throw fileopenexception
}
catch (fileopenexception& Fe)
{
Handle this exception, and if the exception can be recovered well, then the function
return normally; This exception must be thrown again for the call function at the top to be able to
To reason with this unusual object
int result = Reopenfile (f);
if (result = = false) throw;
}
}
void ReadFile (File f)
{
Try
{
Reading data from a file may throw filereadexception
}
catch (filereadexception& Fe)
{
Handle this exception, and if the exception can be recovered well, then the function
return normally; This exception must be thrown again for the call function at the top to be able to
To reason with this unusual object
int result = Rereadfile (f);
if (result = = false) throw;
}
}
void WriteFile (File f)
{
Try
{
Writing data to a file may throw filewriteexception
}
catch (filewriteexception& Fe)
{
Handle this exception, and if the exception can be recovered well, then the function
return normally; This exception must be thrown again to allow the upper-level call function to handle the exception object again
int result = Rewritefile (f);
if (result = = false) throw;
}
}
void Func ()
{
Try
{
To operate on a file, there may be filewriteexception, filewriteexception
and filewriteexception anomalies
OpenFile (...);
ReadFile (...);
WriteFile (...);
}
Note: Fileexception are fileopenexception, filereadexception and Filewriteexception
base class, so the catch (fileexception& FE) defined here captures all the differences from the failure of the file operation
Often.
catch (fileexception& Fe)
{
exceptioninfo* EF = fe. Getexceptioninfo ();
cout << "An unrecoverable error occurred while working on the file because:" << fe << Endl;
}
}
Here are more examples of object-oriented and exception handling combinations:
#include <iostream.h>
Class Exceptionclass
{
char* name;
Public:
exceptionclass (const char* Name = "default name")
{
cout<< "Construct" <<name<<endl;
this->name=name;
}
~exceptionclass ()
{
cout<< "Destruct" <<name<<endl;
}
void mythrow ()
{
throw Exceptionclass ("My Throw");
}
}
Void Main ()
{
Exceptionclass E ("Test");
Try
{
E.mythrow ();
}
catch (...)
{
cout<< "*********" <<endl;
}
}
This is the output information:
Construct Test
Construct my throw
Destruct my throw
****************
Destruct my throw (this is the destructor of the copy of the exception class in the exception handling space)
Destruct Test
======================================
In general, however, we may be more accustomed to writing statements that produce exceptions and exception classes to be throw, and the following code may be more likely to be written:
Class Exceptionclass
{
Public
Exceptionclass (const char* name= "Exception Default Class")
{
cout<< "Exception Class construct String" <<endl;
}
~exceptionclass ()
{
cout<< "Exception Class destruct String" <<endl;
}
void ReportError ()
{
cout<< "Exception Class:: This is the ERROR message" <<endl;
}
};
Class Arguclass
{
char* name;
Public
Arguclass (char* name= "default name")
{
cout<< "construct String::" <<name<<endl;
this->name=name;
}
~arguclass ()
{
cout<< "Destruct String::" <<name<<endl;
}
void Mythrow ()
{
Throw Exceptionclass ("my Throw");
}
};
_tmain ()
{
Arguclass e ("haha");
Try
{
e.mythrow ();
catch (int)
{
cout<< "If this are message display Screen, this is a error!! " <<endl; //This line does not perform
}
catch (Exceptionclass pTest)
{
ptest.reporterror ();
}
catch (...)
{
cout<< "***************" <<endl; &NBSP
}
Output message:
Construct String::haha
Exception Class construct String
Exception class Destruct string
Exception class:: This is the "Error message
Exception class destruct string
Destruct String::haha
Four, exceptions thrown in constructs and destructors
look at the program first, and if I throw an exception at the constructor, the destructor of the class is invoked. But if you do not call, the things in that class will not be released.
#include <iostream.h>
#include <stdlib.h>
class ExceptionClass1
{
char* s;
Public:
exceptionclass1 ()
{
cout<< "ExceptionClass1 ()" <<endl;
S=new Char[4];
cout<< "Throw a exception" <<endl;
throw 18;
}
~exceptionclass1 ()
{
cout<< "~exceptionclass1 ()" <<endl;
Delete[] s;
}
};
void Main ()
{
Try
{
ExceptionClass1 e;
}