Summary of exception handling (III)-cost and advantages

Source: Internet
Author: User

Not every C ++ program should use exception handling, because the exception throws are not as fast as normal function calls, so Exception Handling should be used between different program parts developed independently, used for abnormal communication. If the program implementer encounters an accident that cannot be partially processed, it may throw an exception to notify the caller. Because there is no fixed standard for identifying an exception, the operation that pushes a value when the stack is full can be considered as an exception and throw it out, you can also think that this is a normal action. Expand the stack and then push it. Exception Handling is an auxiliary means for fault tolerance system implementation. Determining whether a program handles exceptions thrown by the database or terminates the program is a major difficulty in the design process.
Some best solutions for exception handling:
1. In a layer or component, not every function can handle exceptions. The try block and catch clause are usually used by the entry point function of a program component.
2. Use the predefined exceptions as accurately as possible for abnormal conditions. Exceptions provided by the system can reduce code and improve code readability (especially when new users do not need to understand custom exception structures ). In most cases, the system provides enough exceptions.
3. collect as much context information as possible when exceptions occur. One reason an exception is better than the return code is that it can type errors and provide much more information than the error code. Therefore, we have no reason not to use this function.
4. Exception Handling is an expensive overhead. It should be used to handle abnormal states rather than to control logical rules.
5. Avoid using exception handling in a loop. If this is the case, place the try/Catch Block outside the loop.
6. For predictable errors that can be avoided through programming, try to use programming methods.
7. Do not rethrow an exception unless necessary, because rethrow also has a price.
8. Always sort exceptions in catch blocks in the most specific to the least specific order. This method handles a specific exception before passing it to a more general Catch Block.
9. Do not derive user-defined exceptions from the exception base class. For most applications, custom exceptions are derived from the applicationexception class.
10. Use the word "exception" as the end of the exception class name.
11. A series of catch blocks can be used to handle different exceptions in different ways. For example, to handle file-related exceptions, catch the following exceptions (filenotfoundexception, directorynotfoundexception, securityexception, ioexception, unauthorizedaccessexception, exception ).

The biggest role of an exception in an organization is that it clearly defines the contractual relationship between class developers and class callers. Therefore, we hope that the class developer can clearly explain under what conditions a method will encounter exceptions. The class caller must ensure that his call has a clear and complete handling of exceptions. To achieve this effect, pay attention to the following points:
1. When designing a class, you must design the exception class at the same time and write instructions for the exception class. (For the class designer, it is necessary to make it easier for others to call, either to return a normal result or to have a type exception .)
2. When describing classes and methods, you must define the exceptions and trigger conditions.
3. Use exceptions in the Code, rather than return values.
4. Determine the input parameters. Incorrect parameters and NULL pointer exceptions are used.
5. test all exceptions once.
6. When calling a method, if there is an exception in the method, you must use the try block for exception handling.

Not every C ++ program should use exception handling, because the exception throws are not as fast as normal function calls, so Exception Handling should be used between different program parts developed independently, used for abnormal communication. If the program implementer encounters an accident that cannot be partially processed, it may throw an exception to notify the caller. Because there is no fixed standard for identifying an exception, the operation that pushes a value when the stack is full can be considered as an exception and throw it out, you can also think that this is a normal action. Expand the stack and then push it. Exception Handling is an auxiliary means for fault tolerance system implementation. Determining whether a program handles exceptions thrown by the database or terminates the program is a major difficulty in the design process.
Some best solutions for exception handling:
1. In a layer or component, not every function can handle exceptions. The try block and catch clause are usually used by the entry point function of a program component.
2. Use the predefined exceptions as accurately as possible for abnormal conditions. Exceptions provided by the system can reduce code and improve code readability (especially when new users do not need to understand custom exception structures ). In most cases, the system provides enough exceptions.
3. collect as much context information as possible when exceptions occur. One reason an exception is better than the return code is that it can type errors and provide much more information than the error code. Therefore, we have no reason not to use this function.
4. Exception Handling is an expensive overhead. It should be used to handle abnormal states rather than to control logical rules.
5. Avoid using exception handling in a loop. If this is the case, place the try/Catch Block outside the loop.
6. For predictable errors that can be avoided through programming, try to use programming methods.
7. Do not rethrow an exception unless necessary, because rethrow also has a price.
8. Always sort exceptions in catch blocks in the most specific to the least specific order. This method handles a specific exception before passing it to a more general Catch Block.
9. Do not derive user-defined exceptions from the exception base class. For most applications, custom exceptions are derived from the applicationexception class.
10. Use the word "exception" as the end of the exception class name.
11. A series of catch blocks can be used to handle different exceptions in different ways. For example, to handle file-related exceptions, catch the following exceptions (filenotfoundexception, directorynotfoundexception, securityexception, ioexception, unauthorizedaccessexception, exception ).

The biggest role of an exception in an organization is that it clearly defines the contractual relationship between class developers and class callers. Therefore, we hope that the class developer can clearly explain under what conditions a method will encounter exceptions. The class caller must ensure that his call has a clear and complete handling of exceptions. To achieve this effect, pay attention to the following points:
1. When designing a class, you must design the exception class at the same time and write instructions for the exception class. (For the class designer, it is necessary to make it easier for others to call, either to return a normal result or to have a type exception .)
2. When describing classes and methods, you must define the exceptions and trigger conditions.
3. Use exceptions in the Code, rather than return values.
4. Determine the input parameters. Incorrect parameters and NULL pointer exceptions are used.
5. test all exceptions once.
6. When calling a method, if there is an exception in the method, you must use the try block for exception handling.

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.