Before discussing C exception processing, let's take a look at a common example in a C language program:
#include
File * fp;
Fp = fopen ("Test.txt", "R");
IF (fp == null)
{
Printf ("can't open file! / n");
exit (0);
}
A robust program needs to consider a lot of factors that make the program failure. If the various factors are judged, the structure of the entire program will become very confusing.
The method of c processing errors is different, it has the following characteristics:
1. No need to mess up the program, if there is no error, the run should not be affected.
2, do not rely on the return value of the function to report whether the error is generated.
3. Even if the bundled manner is used in a centralized manner, the corresponding error handling operation can be performed according to the different types of error.
Let's take a look at how the anomalous mechanism provided in C does this.
/// ----------------------------------- ----------------------------
#include
#include
#pragma HDRSTOP
/// ----------------------------------- ----------------------------
#pragma argsused
Int main (int Argc, char * argv [])
{
Double * data;
Try
{
Data = New Double [1000];
}
Std :: bad_alloc is the only unusual exception that the New operator may
Catch (std :: bad_alloc)
{
COUT << "can't alloc memory!" << endl;
exit (0);
}
Capture all remaining exceptions
Catch (...)
{
}
DELETE [] DATA;
ShowMessage ("OK");
Return 0;
}
/// ----------------------------------- ----------------------------
Using an abnormal syntax is simple, all statements that may generate an exception are included in the TRY block and capture an exception occurring in the subsequent CATCH statement. After a try block, you can follow several different Catch statements to capture different exceptions. If you want to capture any possible exceptions, you should use the catch (...) statement. If you want to create an exception in your own program, you can follow the following methods:
/// ----------------------------------- ----------------------------
#include
#include
#pragma HDRSTOP
/// ----------------------------------- ----------------------------
#pragma argsused
Class Out {};
Int main (int Argc, char * argv [])
{
Try
{
Throw Out (); // throw a temporary OUT object
}
Catch (OUT & E) /// Capture OUT Unusual
{
Cout << "Out Exception throwed!" << Endl;
Throw; // / throw an abnormality outward}
Return 0;
}
/// ----------------------------------- ----------------------------
Use the keyword throw to throw any object. This object does not need to inherit the Exception class. Even the basic type data can also be thrown as the parameters of the Throw. In the parameter of Catch If a similar variable declaration is included, this exception object can be used in the CATCH block. If the exception caused in the CATCH block, the consequences of exceptions cannot be completely eliminated, then the CATCH statement can be called again in the catch statement, so that the capture is propagated to the outer layer. If there is no exception that occurs in a function, the exception will propagate into the code segment calling this function. text