Exception and error dispose

Source: Internet
Author: User

From: http://blog.csdn.net/zdl1016/archive/2007/06/27/1668622.aspx

 

Use exceptions and return values

 

1. Using the return value to handle errors requires a strict programmer's programming style. No matter whether the programmer has this habit or not, this is very hopeless.

 

2. Correct exception handling is a common sense in C ++. By sending an error signal, the program code and the error handling code can be separated, and the program will not ignore the error.

 

3. You must know which error handling method is used, and the return value or throw an exception. If you do not know, your program will be faulty.

 

4. exceptions are raised and processed based on each thread. Exceptions cannot be ignored by the thread and must be processed. Unprocessed exceptions can end the process, not just the end of the thread; exception Handling releases all stack objects when releasing the stack, avoiding resource vulnerabilities. Exception Handling requires a large number of additional operations, making it not suitable for frequently Running code. In details, catch blocks have some overhead, but try blocks have very little overhead. Therefore, only when an exception is thrown will there be a lot of exception operation overhead; you can throw any type of exception object, excluding integers.

 

5. the return value can indicate normal and abnormal function operations, but cannot prevent the thread from continuing to run. The return value is easily ignored. The return value is an integer in typical cases, normally, the ing conforms to a predefined value. The returned values can be efficiently transmitted and received;

 

6. All non-error status information should use the return value.

 

7. return values are used for errors that can be ignored without any problems in most cases.

 

8. The error handling in the loop must be fast. Because of the extra overhead of exceptions, it is a better choice to use the return value for better performance. In this case, if you really want to use an exception, you can create a function to convert the returned value to an exception.

 

9. Errors used in the intermediate language module.

 

10. Use the error handling mechanism of Windows API. Use setlasterror to set the error code and getlasterror to detect the error code.

 

11. From the perspective of C ++ Exception Handling, faults should be considered as errors.

 

12. You must use the/EHA debugger option to obtain operating system exceptions using the C ++ exception mechanism.

 

14. windows structure Exception Handling: Use the _ Try, _ Wait T, _ Finally, _ leave keywords and raiseexception API functions. Supported by windows and not suitable for other operating systems; do not parse the C ++ object. It is thrown as a result of a hardware exception or an operating system exception, or as a result of the raiseexception function.

 

15. c ++ Exception Handling: Use keywords such as try, throw, and catch, supported only by C ++, and parse C ++ objects; can throw any type of C ++ object. Exception objects can be derived from standard basic exception classes, or from any classes, or they can also be built-in types. They are thrown as results of throw statements.

 

16. Visual C ++ uses the structure exception handling mechanism to implement C ++ exceptions.

 

17. Structure Exception Handling cannot process Object Parsing. Therefore, you should always use C ++ exceptions in the C ++ program. However, because a C ++ exception cannot handle hardware or operating system exceptions, your program needs to convert the structure exception to a C ++ exception.

 

18. to correctly handle hardware and operating system exceptions, you can create your own exception classes and use the _ set_se_translator function to install a structure exception to the C ++ exception converter.

 

19. Do not flat the converted structure exceptions that cannot be recovered.

 

20. When exceptions are rarely thrown, the cost of using exceptions is not very high, and this can indeed improve performance.
(I add one. If There may be various errors, but you cannot clearly know where the error is, you can throw an exception)

 

21. The most important part of an exception policy is actually a policy. Do not make up for it later.

 

22. Exception flat rules: the flat processor is provided in order. If the flat processor flat the pointer to the same type or to the same type of throw object, the exception should be flat. If the attacker obtains a public base class or a pointer to a public base class to throw an object, an exception should be thrown. If the attacker ignores the exception, the attacker can throw any type of exception, therefore, it is always placed at the end.

 

23. Define an exception base class to handle exceptions thrown by program code.
Class cprogramexception: Public exception {
Public:
Cprogramexception (const _ exstring & _ what_arg ):
Exception (_ what_arg ){}
};
Using the cprogramexception class makes Exception Handling easier, because exceptions in all programs can be obtained by handling this base class. If necessary, you can also use additional member data to fully describe specific issues.

 

24. Use auto_ptr or a similar pointer class to automatically release resources by limiting the Dynamic Allocation of local variables:
Void leakfreefunction (INT Arg ){
Auto_ptr <cmyobject> pobject (New cmyobject (ARG ));
... // Do something that throws an exception
// Can still call member functions as normal
Pobject-> memberfunction ();
// No need to delete pobject
}
Note: auto_ptr is used only when Delete is used to release resources.

 

25. Exception Handling is simpler, more reliable, and more effective, and more robust code can be created. However, you should only use exception handling in case of exceptions. If you think a pointer should be a time-space value, in this condition, you can directly check this value in the Code, rather than using exceptions.

 

26. Non-mfc c ++ exceptions should be obtained through reference. You do not need to delete the exception object when you use a reference to throw an exception (because an exception caused by a reference is transmitted in the stack ), and it retains polymorphism (so the exception object you throw is exactly the exception object you throw ). When you use a pointer to throw an exception, you need to delete the object. When you use a value to flat the object, the object will be "slicing". That is to say, the derived exception object will be converted to the final data type.

 

27. The MFC exception should be flushed by pointer. Because they are usually allocated from the heap, after you handle the exception, you need to call the delete member function:
...
Catch (cfileexception * E ){
// Handle file exception
...
E-> Delete (); // required to prevent a memory leak
}
Therefore, you cannot use the omitted flat processor to throw an MFC exception because it may cause a memory leak. You must use the delete member function to delete an MFC exception instead of the delete operator, because some MFC exceptions are created as static objects.

 

28. Once an exception is thrown, you can handle it by performing a combination of the following typical actions:
(1) do nothing.
(2) modify the problem and try the code again.
(3) modify the problem but do not retry the code.
(4) If necessary, display the error message to the user.
(5) If the problem is not a program error, output a Diagnostic Trace message.
(6) If the problem is a program error, output an asserted.
(7) record this problem in the log file.
(8) If the exception is unrecoverable, stop the process.
(9) sort out allocated resources.
(10) re-Throw this exception so that advanced functions can handle this exception, especially when the current function cannot be completely resolved. You can throw the same exception object or a new exception object.

 

29. Synchronization exceptions (/EHS) are opposite to/EHA, rather than/GX. /GX is actually a simplified form of/ESCs. /GX indicates that the compiler should assume that the function of extern "C" does not throw a C ++ exception, while/EHS does.

 

30. Record exceptions for users and call environments. Generally, the exception object type is used to notify the calling environment of problems, and the description string of the problem is used to notify the user.

 

31. By default, new and malloc do not throw exceptions for errors in Visual C ++, but you can use _ set_new_handler to install a processor so that new can throw exceptions for errors. You can also enable malloc to use the same processor by calling _ set_new_mode.
# Include <New. h>
Class bad_alloc: Public exception {
Public:
Bad_alloc (const _ exstring & what_arg): exception (what_arg ){}
};
Int newhandler (size_t size ){
Throw bad_alloc ("operator new couldn't allocate memory ");
Return 0;
}
Int apientry winmain (hinstance, hinstance hprevinstance,
Lpstr lpcmdline, int ncmdshow ){
_ Set_new_handler (newhandler );
_ Set_new_mode (1); // use newhandler for malloc as well
...
}

 

32. If the new value is not set in the existing code, you should always throw an exception when a new error occurs.

 

33. the floating point number is different from the integer. By default, it is not subject to any exception, but a very strange value "1. # Info "(it indicates that this value is not a number ). To make it easier to detect floating point numbers, you should use the following code to throw an exception in a floating point number error.

# Include <float. h>
Int CW = controlfp (0, 0 );
CW & = ~ (Em_overflow | em_underflow | em_inexact | em_zerodivide | em_denormal | em_invalid );
_ Controlfp (CW, mcw_em );
The floating point exception processor must call _ clearfp as its first command to clear the floating point exception.

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.