The attitude of the wrong

Source: Internet
Author: User
Error nearly a year to work relationship to see the code of other programmers more opportunities to learn a lot of things, but also found a lot of problems, which I encountered the most is the wrong attitude to deal with.

1) never intercept errors;

This may be the most primitive and the most bad behavior, they always think their program is sure 100% will not be a problem, because they believe that their code to consider all the possible circumstances, this understanding I think is very dangerous, to flip through your code to see if the following common situations are considered:

In a text box that only allows you to enter numbers, you block keyboard events, but does your program work correctly when the user pastes the text with the right button?

L SET the locked properties of the text box users can paste, your program is wrong?

L Low disk space, directory read-only, no permissions, a decimal point in the directory name, the system adopted a short date or even non-standard format;

L Obviously checked the disk space in advance, what disk quotas can be used by the user, or the disk space is not enough;

L users in your long hours of operation of the medium of impatience forced shutdown after the operation of your program;

Do you believe that the Access database or the SQL Server database Settings field cannot be empty, but the database will have a inexplicably null value inside;

There are so many things that you can't imagine, so you'll never think of everything.

2) always intercept errors;

This situation is the opposite of the first case, he will take pains to add On error Goto in each process, then make an error quote a MsgBox box out, I believe that two years or more than five years of work experience 99% of VB programmers are basically doing so, Perhaps immediately someone stood up and said: "You just said you are not likely to consider each situation, so to stop the wrong, why immediately back out?" Here I would like to present my first important point:

Try not to block any low-level errors.

We assume that there are 4 process a,b,c,d, where a called the b,b has called the C,c and called D, and if we intercept the error in each of the children, then if D is wrong, the program will report an error, obviously when D exits, the code behind C should not be executed, It seems like the only way he's going to do it now is to return false by D to indicate failure, and he knows that the underlying failure is not going to continue, and that both C and B should return false to cancel the task.

God, this is not going to be written without a process. Return true and False, look at your program, is this possible? And you're going to have a very embarrassing situation where you call any of these functions:

If B = False Then

A = False

Exit Sub

End If

I'm sure you feel bad, not only that you have a fatal problem: the top-level program (note is the program rather than the user) at all do not know where the bottom of the failure, what reason failed, the only thing he knew was a failure! Ah, that's terrible.

So let's look back at the bottom without blocking the error:

Public Sub A ()

On Error Goto ErrorHandler


B ()

' Do some thing

Exit Sub


ErrorHandler:

MsgBox (Err.Description)

End Sub


Public Sub B ()

' Do some thing

C ()

End Sub


Public Sub C ()

' Do some thing

D ()

End Sub


Public Sub D ()

' Do some thing

End Sub

Here B, C, d do not intercept the error, and do not need to return the success, they only focus on their work, and you can see, any error will jump back to the previous level.

A mistake is a failure of a complete business, no matter what the situation is. A database connection failure is an error, and something that does not conform to the business rules is also an error and should be triggered in our program as well. For example:

If strName = "" Then

Err.Raise (vbObjectError +), "Cworker.update", "name cannot be empty." ")

End If

Of course I also put the user's cancellation as an error, triggering the cancellation error, for example:

If Usercancel Then

Err.Raise (vbObjectError +, "Frmworker.cmdcancel_click", "user cancel operation")

End If

I am generally accustomed to dividing mistakes into fatal, hardware and software errors, cancellation errors, and business errors. Business errors are divided into: do not conform to business rules errors, permissions errors.

I usually divide all functions into: security functions and unsafe functions, such as 10 kinds of actions in a window, I first establish a runfunction process, for example:

Public Sub runfunction (ByVal vfun as Enumbusinfunction)

This process acts as the entry point for all actions and unifies the error, which is called a security function, because it is not theoretically possible to advertise the error, and then call this function in all events. Other functions that are invoked in the Runfunction function should theoretically be unsafe functions.

The above is my attitude to the wrong, said very messy, please do not mind, I very much hope that we can talk about this issue.

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.