[Code robustness] learn to pay attention to both the front and back sides of the Code to improve system robustness.

Source: Internet
Author: User
RobustProgramThe exception is not handled, but can be handled and located!

 

When I consulted the customer last year, the customer raised a very interesting question: an application system they made often fails, and most importantly, the cause of the problem cannot be found.

View later Code When writing code, you can see code similar to the following. 1 Ipageflowservice pageflowservice =
2 Bundleruntime. getfirstordefaservice service <ipageflowservice> ();
3 // Redirect to first node.
4 Response. Redirect (pageflowservice. firstpagenodevalue );

Under normal circumstances, this code can pass normal functional tests without any problems. However, users' thinking is different from that of programmers. Therefore, strange issues are possible. In this case, "Nothing is impossible !", :).

 

Let me explain the possible problems with this code. (1) It is possible that the pageflowservice is not obtained, and the program will throw nullreferenceexception; (2) Pageflowservice. firstpagenodevalue may be null. At this time, the program will also throw nullreferenceexception.

 

When any of the above situations occurs, the system will crash. Maybe some programmers have noticed that nullreferenceexception may occur, so they changed the program to the following. 1 Ipageflowservice pageflowservice =
2 Bundleruntime. getfirstordefaservice service <ipageflowservice> ();
3 // Redirect to first node.
4 If (Pageflowservice! = Null &&! String . Isnullorempty (pageflowservice. firstpagenodevalue ))
5 {
6 Response. Redirect (pageflowservice. firstpagenodevalue );
7 } The chance of nullreferenceexception in this code is almost zero, which means that the above program will no longer encounter exceptions during running. It seems to be much more robust.

However, on the contrary, this is even more tragic because once pageflowservice is null or pageflowservice. when the firstpagenodevalue is null, the above program will not encounter any exceptions, but it makes it difficult for us to find out what went wrong in case of system problems.

 

Therefore, the correct method should be for the opposite situation, and our program must also handle it, and a good exception information should be thrown, as shown below. 1 // Get pageflowservice.
2 Ipageflowservice pageflowservice =
3 Bundleruntime. instance. getfirstordefaservice service <ipageflowservice> ();
4 If (Pageflowservice = Null )
5 {
6 Throw New Servicenotavailableexception (
7 Typeof (Ipageflowservice). fullname, properties. Resources. iopenworkswebshellname );
8 }
9
10 If ( String . Isnullorempty (pageflowservice. firstpagenodevalue ))
11 {
12 Throw New Pagenodeexception (properties. Resources. cannotfindanavailablepagenode );
13 }
14 // Redirect to first node.
15 Response. redirect (pageflowservice. firstpagenodevalue); in the above Code, if pageflowservice is null, A servicenotavailableexception is thrown to describe exceptions that do not exist in the service. When exceptions occur, we can quickly identify the root cause of the problem; in pageflowservice. if firstpagenodevalue is null, pagenodeexception is thrown. In this way, when an exception occurs, we can quickly locate the problem. In the above example, you can also introduce assertions to achieve the same effect. Here, I will just give an example to describe it.

 

To provide the robustness of the system, when writing a program, we must pay attention to both the positive and negative behaviors provided and provide different solutions! Learn to handle exceptions habitually !!

 

Here are some suggestions.

 

1. Do not swallow exceptions. Learn to get used to handling exceptions, that is, consider both positive and negative situations. Although writing programs in this way is easy, once the problem occurs, it also makes you very troublesome. Generally, the following exceptions are swallowed up: (1) Try... Catch ..., No exception is processed. In this case, all exceptions are ignored. It is very easy to write code, but after the problem occurs, the error cannot be found. Try
{
}
Catch
{
// Ignore the exceptions
} (2) use try... Catch is only a general processing. Here, the processing method is better than the first one, but the program does not provide different processing methods based on different types of exceptions captured. Although it can locate the approximate cause, it cannot be precise, therefore, this method still has problems. Try
{
}
Catch (Exception ex)
{
// Show the ex. message here.
} (3) only deal with positive conditions. This method is similar to (1). Of course, the motivation is different. In this case, when writing a program, it is assumed that the program is normal, do not consider exceptions. If (Pageflowservice! = Null &&! String . Isnullorempty (pageflowservice. firstpagenodevalue ))
{
Response. Redirect (pageflowservice. firstpagenodevalue );
} 2 introduce logs here. It is best not to use logs. message/error (sting message) processing method, because such logs cannot accurately describe the results produced by the thread, class, and method. Therefore, I advocate direct use of log4net logs. 3. modular decomposition of programs this method can restrict dependencies in a functional module. Therefore, the scope of exceptions can be further restricted to help better locate the root cause of the problem. You are welcome to add more.

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.