VB icon

Central Error Handler

Email
Submitted on: 1/6/2015 8:38:00 AM
By: CygnusX1 (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: VB 5.0, VB 6.0
Views: 1433
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Central Error Handler
' Description: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.
' By: CygnusX1 (from psc cd)
'
' Assumes:The appropriate comments have been added to the code so that you know where to place each item. So long as you follow that the code will work great for anyone.
Note that the Public Function Error Handler can accept any additions concerning the expansion of error codes. All functions/procedures/events that use this error handler do not need to be updated.
'**************************************

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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.