Handle errors used in VB6

Source: Internet
Author: User

Will error capture affect the speed?

Yes, using error handling will reduce some performance. Net, using Try Catch will also affect the performance. Therefore, when we use Reflector to decompile Microsoft class libraries, we will find that its sub-process rarely uses Try to capture errors, generally, you can use predictable methods to determine possible errors and handle them accordingly.

In fact, after error capture is used, the compiled code actually does a lot of operations that we don't know about. Although it facilitates the encoding process to a certain extent, but it sacrifices a certain speed, which has advantages and disadvantages. Therefore, we should use error capture correctly.

Tested VB6 code

  

Option Explicit

  

Dim cn As New ADODB. Connection

Dim rs As New ADODB. Recordset

Dim m_FldsName () As String

  

Private Sub commandementclick ()

  

Dim II As Long

Dim t As Single

  

Cn. Open "Provider = SQLOLEDB.1; Persist Security Info = False; User ID = sa; Initial Catalog = guangshang; Data Source = SUPER"

Rs. Open "Select * from FldSet", cn, adOpenKeyset

  

Do Until rs. EOF = True

Rs. MoveNext

Loop

  

Rs. MoveFirst

T = Timer

  

ReDim m_FldsName (rs. Fields. Count)

For II = 0 To rs. Fields. Count-1

M_FldsName (II) = rs. Fields (II). Name

Next

  

Do Until rs. EOF = True

ReadFieldsNotOnErr "Err1"

ReadFieldsNotOnErr "TableName"

ReadFieldsNotOnErr "Err2"

Rs. MoveNext

Loop

MsgBox "NotErr:" & Timer-t

On Error Resume Next

Rs. MoveFirst

T = Timer

Do Until rs. EOF = True

ReadFieldsOnErr "Err1"

ReadFieldsOnErr "TableName"

ReadFieldsOnErr "Err2"

Rs. MoveNext

Loop

MsgBox "OnErr:" & Timer-t

  

Rs. Close

Cn. Close

  

End Sub

  

Private Sub ReadFieldsOnErr (FieldName As String)

Dim v As Variant

V = rs (FieldName). Value

End Sub

  

Private Sub ReadFieldsNotOnErr (FieldName As String)

Dim II As Long

Dim IsExists As Boolean

Dim v As Variant

IsExists = False

  

For II = 0 To UBound (m_FldsName)-1

If m_FldsName (II) = FieldName Then

IsExists = True

Exit

End If

Next

  

If IsExists = True Then

V = rs (FieldName). Value

End If

End Sub

  

==========================================

  

Test results:

No error

ReadFieldsOnErr: 0. 46

ReadFieldsNotOnErr: 0. 47

Incorrect

ReadFieldsOnErr: 0.96

ReadFieldsNotOnErr: 0.47

We can see that error handling is not used, and the speed is quite stable. When error handling is used, the speed gap is large when errors exist. Therefore, we recommend that you pre-process possible errors in subprocesses that are frequently called in loops to reduce the use of On error to increase the speed.

Related Article

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.