Computing.Net > Forums > Programming > On Error in VB

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

On Error in VB

Reply to Message Icon

Name: abolk
Date: January 14, 2004 at 07:50:33 Pacific
OS: xp
CPU/Ram: gig
Comment:

I am fairly new with VB and am trying to work on some error handling. I have a record set which is a list of jobs that the code will run. What is happening now is if 1 job fails, the whole process fails. What I want to happen is if a job fails, log it and then move on to the next job.

What I want to do is when the code encounters and error, go to a function that handles the error, create a log, move to the next record in the record set and then resume the code. Could somebody please help me out with some code for this?

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: AlwaysWillingToLearn
Date: January 14, 2004 at 08:54:32 Pacific
Reply:

Well One way to do this would be;
within a modual make an error handeler function thus

public function ExceptionHandeller(ErrNum as integer, Desc as string)

Dim TheFile As String
Dim FileNumber

'The path for the log file
TheFile = App.Path & "\Log.txt"

'Any available fileNumber
FileNumber = FreeFile

'Open the log file
Open TheFile For Output As #FileNumber
print #freefile, "Error Number: " _
& ErrNum & " " & "Description: " & desc
close #filenumber

End Function


In your code all you need to do is when an error occurs call this function and pass the arguements to it like this:

'For example if your sub was called calculate
'Note the on error resume next statment is not very good to use in programming but this is one way of doing it.

private sub Calculate()
On Error Resume Next

''''YOUR PROCESSSS''''

if err then
ExceptionHandeller(err.Number,
err.descripton)
End if

End sub

Try this it should work
:)


0

Response Number 2
Name: StuartS
Date: January 14, 2004 at 11:54:31 Pacific
Reply:

Its a weird beast is the VB Error Handler and it doesnt always work the way you expect.

Unfortunatley, as written the ExceptionHandler will never get called. Putting On Error Resume Next at the beginning of the module and VB will do just that, Resume at the next command clearing the error.

Replace On Error Resume Next

with

On Error Goto ErrorHandler:

This is the only time in VB where you would use the Goto Statement.

At the end of the module insert

Exit Sub

ErrorHandler:
ExceptionHandeller(err.Number,
err.descripton)
Resume Next

End Sub


Put Exit Sub before the ErrorHandler as it will go crashing through and execute the ExceptionHandler even when there is no error.

Note the colon at the end of ErrorHandler. This defines ErrorHandler as a label.

Stuart


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: On Error in VB

Error in vb www.computing.net/answers/programming/error-in-vb/1236.html

winsock error 10054 in VB www.computing.net/answers/programming/winsock-error-10054-in-vb/4027.html

Run time Errors in VB www.computing.net/answers/programming/run-time-errors-in-vb/3981.html