Advertisement
Java_Volume1 Debugging and Error Handling #96529

Central Error Handler

The following code provides the means of adding a Centralized Error Handler to an application. I keep the Public Function in a Module named ErrorHandling to keep things simple.

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
Option Explicit

'The Following Code gets added to each Sub in which you
'Would like to trap errors in. 
ErrHandler:
    Dim iErrorAction As Long
    iErrorAction = ErrorHandler(Err)
    Select Case iErrorAction
    Case 1
      Resume
    Case 2
      Resume Next
    Case 3
    'Case 3 is for Resume to a Line, otherwise left blank
    Case 4
      Exit Sub
    Case 5
      End
    End Select
'The code below remains in a Module where it can be expanded in one central location
Public Function ErrorHandler(iErrNum) As Long
Dim iAction As Long
  Select Case iErrNum
    Case -2147467259
    MsgBox "A database data entry violation has occurred. " & "Error Number = " & iErrNum
    iAction = 5
    Case 5
    'Invalid Procedure Call
    MsgBox Error(iErrNum) & " Contact Help Desk."
    iAction = 2
    Case 7
    'Out of memory
    MsgBox "Out of Memory. Close all unnecessary applications."
    iAction = 1
    Case 11
    'Divide by 0
    MsgBox "Zero is not a valid value."
    iAction = 1
    Case 48, 49, 51
    'Error in loading DLL
    MsgBox iErrNum & " Contact Help Desk"
    iAction = 5
    Case 57
    'Device I/O error
    MsgBox "Insert a disk into Drive A."
    iAction = 1
    Case 68
    'Device Unavailable
    MsgBox "Device is unavailable(the device may not exist or it is currently unavailable)."
    iAction = 4
    Case 482, 483
    'General Printer Error
    MsgBox "A general printer error has occurred. Your printer may be offline."
    iAction = 4
    Case Else
    MsgBox "Unrecoverable Error. Exiting Application. " & "Error Number = " & iErrNum
    iAction = 5
    End Select
    ErrorHandler = iAction
  End Function
Bình luận gốc (3)
Được khôi phục từ Wayback Machine