In the process of software development, error capture is particularly important, because some errors may lead to abnormal software functions, while some may cause destructive losses. There is no error-free software in the world. Software logical errors, human operation errors, changes in operating conditions, and other factors can cause exceptions. The following code is an example:
Char * pszData = NULL; // suppose it is a global variable
BOOL ReadData (void)
{
FILE * pFile = fopen ("c: \ data. dat", "r ");
// Assume that the length of the c: data. dat file is 1024 bytes.
If (pFile! = NULL)
{
If (pszData)
Delete pszData;
PszData = new char [1024];
If (1024 = fread (pszData, pFile ))
Return TRUE;
}
// An error occurred while opening the file, or the file length is insufficient
Return FALSE;
}
Void PrintData ()
{
For (int I = 0; I <1024; I ++)
{
Printf ("% x", pszData [I]);
}
}
It should be okay to look at this piece of code, because this section of code has undergone error processing and an error is returned when the operation fails. HoweverThere is a hidden danger in PrintData. What if pszData is NULL?