Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 49,596 times

Contents

Related Categories

Handling Errors in VB/VBA/VBS/ASP - Real-life Samples

mrjdesign

Real-life Samples

…Or simply put the reusable code appendix.

Get a full file as string to your application

Public Function GetAFile(ByRef TheFile As String) As String
On Error GoTo ErrHandler
Dim F As Integer 

F = FreeFile ' get a free file number (always in the range 1 - 255)
Open TheFile For Binary As #F 'open the file in binary mode
GetAFile = Input(LOF(F), #F)
Close #F ' close the file when your done


Exit Function ' if there was no error, don’t step further than here
ErrHandler: ' this is a named range called ErrHandler

MsgBox "An error happened here!" & vbCrLf & _
"Error number: " & Err.Number & vbCrLf & _
"Error Description" & Err.Description & vbCrLf & _
"Error Help Context ID" & Err.HelpContext & vbCrLf & _
"Error Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"

Err.Clear ' destroy the error and…
Resume Next
End Function



Retrieve Error data pertaining to a given error number.

'// Form Code
'Create a form and ad a command button named Command1

Private Sub Command1_Click()
    Call GetErrData(TheNumberHere)
End Sub

'// Module Code

Public Function GetErrData(ByRef ErrorNumber As Long)
On Error GoTo ErrHandler

Err.Raise (ErrorNumber) ' this brief call to the event handler Raises the error

' Note that the Err.Source would be of little help here since
' this would be the calling project that you are in.

Exit Function
ErrHandler:
MsgBox "This is the error info..." & vbCrLf & _
"Number: " & Err.Number & vbCrLf & _
"Description " & Err.Description & vbCrLf & _
"Help Context ID" & Err.HelpContext & vbCrLf & _
"Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
Err.Clear   ' destroy the error and…
Resume Next
End Function


Get all errors printed to a text file, if a proper description exists

The following function will print out all available errors to a text file called AllErrors.txt in the same location as your application file is stored.

'// Form Code
'Create a form and ad a command button named Command1

Private Sub Command1_Click()
    Call GetAllErrors()
    MsgBox "The Error document has been created", vbExclamation, "Finished!" End Sub

'// Module Code

Public Function GetAllErrors()
'clean out residual error tags
Err.Clear
On Error GoTo ErrHandler
Dim I As Long, F As Integer, ErrFile As String
Dim MessageTxt As String

ErrFile = App.Path & "\AllErrors.txt"

F = FreeFile
Open ErrFile For Output As #F
Print #F, "Err.Number|Err.Description|Err.HelpContext|Err.HelpFile"

    For I = 1 To 65535 'all possible valid Long err numbers 'Loop the numbers and call them one by one Err.Raise (I)
GoHere:
' On resuming after getting the error message, go here
   
'only print the messages clearly defined
If MessageTxt <> "" And _
InStr(1, Trim(MessageTxt), "Application-defined or", vbTextCompare) = 0 Then
    Print #F, MessageTxt
End If

    Next I
Close #F

Exit Function
ErrHandler:
MessageTxt = Err.Number & "|" & _
Err.Description & "|" & _
Err.HelpContext & "|" & _
Err.HelpFile
Err.Clear
Resume GoHere   ' Resume execution at the GoHere line label
End Function


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, withou

Comments

  • How to handle error in infinite loop

    Posted by pmanojtvm on 26 Apr 2005

    Is there any way to handle errors in INFINITE LOOP.

    for example,

    While Not blnIdeaFound
    blnIdeaFound = ProcessDetector(EXE_NAME, False)
    Wend

    IF THE CONTROL IS INSIDE THE LOO...