Exception capture and handling

Source: Internet
Author: User

 

  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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.