Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

Well One way to do this would be;
within a modual make an error handeler function thuspublic 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 #filenumberEnd 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 subTry this it should work
:)

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 NextEnd 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

![]() |
![]() |
![]() |

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