- On error Goto 0
Tác giả: truongphu & Sưu tầm
Cấp độ bài viết: Căn bản
Tóm tắt: Bẫy lỗi và Thông báo lỗi (Error Numbers)
A- Bẫy lỗi
Cách 1:
On Error GoTo <Tên nhãn>
<Các câu lệnh có thể gây ra lỗi>
- Nếu một lệnh trong <Các câu lệnh có thể gây ra lỗi> thì khi chương trình thực thi đến câu lệnh đó, chương trình sẽ tự động bỏ qua câu lệnh bị lỗi và nhảy nhanh tới nhãn lỗi.
Cách 1 nầy thường sử dụng khi ta lường trước lỗi, vd: sai đường dẫn tập tin.
Kết thúc các câu lệnh ở trên, nên thoát khỏi Sub (Function) để nếu không có lỗi thì máy tính không thi hành các lệnh trong nhãn lỗi: nếu là các lệnh đặc biệt, vd MsgBox...
Exit Sub (Function)
(Đương nhiên, nếu nhãn lỗi chỉ có chứa lệnh Exit Sub thì câu trên khỏi viết)
<Tên nhãn>: Để cuối Sub (Function)
<Các câu lệnh xử lý lỗi>
vd: If Err.Number<>0 then MsgBox "Error!"
vd: End
End Sub (Function)
Trong một sub (function), ta có thể đặt nhiều bẫy lỗi nêu trên.
Cách 2:
On Error Resume Next
<Các câu lệnh có thể gây ra lỗi>
- Nếu một lệnh trong <Các câu lệnh có thể gây ra lỗi> thì khi chương trình thực thi đến câu lệnh đó, chương trình sẽ tự động bỏ qua câu lệnh bị lỗi và thực thi câu lệnh kế tiếp.
Thông thường, nếu không dự đoán lỗi xảy ra, chúng ta hay dùng cách 2 nầy, đương nhiên là "yếu cơ?" hơn cách 1
B- Thông báo lỗi (Error Numbers)
Code sau đây sưu tầm trên mạng. Gồm List1, 3 command 1, 2 và 3.
Tôi có bổ sung dòng 13: On Error Resume Next
Điều kỳ diệu là chỉ bấy nhiêu code, Khai thác thư viện "kernel32" để ra một trời Error Numbers!
Mã: Chọn hết
- Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
- Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
- Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
- Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000
- Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
- Dim hMod As Long
-
- Private Function ApiErrorText(ByVal ErrNum As Long) As String
- Dim msg As String
- Dim nRet As Long
- msg = Space$(1024)
- nRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrNum, 0&, msg, Len(msg), ByVal 0&)
- If nRet Then
- On Error Resume Next 'thêm vào vì test o' nhà có lôi ngay câu sau
- ApiErrorText = Left$(msg, nRet - 2)
- Else
- ApiErrorText = ""
- End If
- End Function
-
- Private Function VBErrorText(ByVal ErrNum As Long) As String
- VBErrorText = Error(ErrNum)
- End Function
-
- Private Function InetErrorText(ByVal ErrNum As Long) As String
- Dim msg As String
- Dim nRet As Long
- msg = Space$(1024)
- nRet = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, ByVal hMod, ErrNum, 0&, msg, Len(msg), ByVal
-
- 0&)
- If nRet Then
- InetErrorText = Left$(msg, nRet - 2)
- Else
- InetErrorText = ""
- End If
- End Function
-
- Private Sub Command1_Click()
- Me.Caption = "API Errors"
- Dim strErr As String
- List1.Clear
- Me.MousePointer = vbHourglass
- For i = 1 To 6200
- strErr = RemoveCrLf(ApiErrorText(i))
- If strErr <> "" Then List1.AddItem CStr(i) & ": " & strErr
- Next i
- Me.MousePointer = 0
- End Sub
-
- Private Sub Command2_Click()
- Me.Caption = "VB Errors"
- Dim strErr As String
- List1.Clear
- Me.MousePointer = vbHourglass
- For i = 1 To 1000
- strErr = VBErrorText(i)
- If Left$(strErr, 11) <> "Application" Then List1.AddItem CStr(i) & ": " & strErr
- Next i
- Me.MousePointer = 0
- End Sub
-
- Private Function RemoveCrLf(SrcStr As String) As String
- Dim pos As Integer, tmpStr As String
- pos = 1
- RemoveCrLf = SrcStr
- Do
- pos = InStr(pos, RemoveCrLf, Chr$(13))
- If pos Then RemoveCrLf = Left$(RemoveCrLf, pos - 1) & " " & Mid$(RemoveCrLf, pos + 2)
- Loop While pos <> 0
- End Function
-
- Private Sub Command3_Click()
- Dim strErr As String
- Me.Caption = "Inet Errors"
- hMod = LoadLibrary("wininet.dll")
- List1.Clear
- Me.MousePointer = vbHourglass
- For i = 12000 To 12200
- strErr = RemoveCrLf(InetErrorText(i))
- If strErr <> "" Then List1.AddItem CStr(i) & ": " & strErr
- Next i
- FreeLibrary hMod
- Me.MousePointer = 0
- End Sub