Program Robustness: Normal, error, and exception

Source: Internet
Author: User

 


Accurate and meticulous error handling is an important level to ensure program robustness. --------- Introduction


It is easy to distinguish between normal and wrong, but it is not easy to distinguish the difference between errors and exceptions. In fact, it is easy to tell from the probability of things. Whether it is normal, error, or exception, it is a possibility of a thing, but usually the probability of a normal situation is more, followed by errors, exceptions are very small. However, a small probability event also occurs.

For example, write a program related to the weather. The weather can be sunny, cloudy, rainy, or snowy, and sometimes there may be rain or rain. Sunny, cloudy, rainy, and snowy days can be considered normal, but acid rain is a wrong situation, because it is not expected; both sunny and rainy weather will happen less, it can be seen as an unexpected exception. Think about the original bug. It is said that the bug was stuck in the machine. This was an unexpected event, which can be called an exception. Unexpected events can include both normal and wrong events.


To sum up, both normal and wrong events are expected, with a high probability of occurrence. Exceptions are usually unexpected, with a low probability, but may also happen. The difference between normal and wrong is that one is expected and the other is not expected.

PS: Some people may ask: What is the expected thing that has a low probability of occurrence? ------ Of course surprise!

Identifying the similarities and differences between normality, errors, and exceptions is essential for compiling robust programs. Generally, we pay more attention to the normal situation. It is regarded as a "function or feature" that must be completed, which is the minimum requirement. errors are not expected, but it must also be processed. Exceptions are unexpected and must be prevented. The normal situation is usually the main control flow of the program, the error situation as the branch of the program, and exception handling as a high-level alert. The program structure is basically like this:

String result = default_value; try {dosome1 (); // normal stream if (error) {// handle the branch of the error, which is equivalent to the return error_info;} return result ;} catch (someexception SE) {// exception logger. error (E); Return exception_info;} finally {cleanup ();}

1. Do not set the initial value. It is best to set it to the default value.

2. The try {} block contains normal and error handling. The normal case is the master control flow, and the error case is the branch processing (as expected, it must be handled ). Exceptions are beyond try, because they are unexpected and must be prevented.

3. No matter what error occurs at the underlying layer, the upper layer always returns a reasonable value. All the situations, including exceptions, are also handled.

4. Use the Wei Shu sentence. Error Handling always causes a lot of IF-Else To reduce program readability. Comparison:

If (normal) {donormal () ;}vs: If (someerror) {dosomeerror (); Return some_error_info1 };

Elseif (someerror) {dosomeerror () ;}if (someerror2) {dosomeerror2 (); Return some_error_info2 };

Else {dosomeerror2 ();} // continue normal doing.

Anyway, I prefer the structure presented by the latter.

5. If an error occurs, some information will always be returned and handled by the senior management. There is always a layer that can be properly handled. You can return the default value, throw an exception, or submit it to a higher layer, or ignore nothing. Always take a measure.

6. Because errors involve the transfer from the bottom layer to the top layer, or even more than one layer, they are a problem of the overall architecture. It should be carefully designed as a whole.

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.