Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling

Status
Not open for further replies.

ecojohnson

Programmer
Jul 2, 2001
54
US
I am new to Visual Basic (one week experience). I have a program that utilizes winsock. The program is the "sender" which sends data to a remote computer. It's incredibly complicated, but works rather well.

However, there is one nagging problem. Our users tend to occasionally get a Run-time error "52" Bad file name or number error. This stops the entire "sending" process, and the user must click [OK] at this message, restart the sender, and everything is back to normal.

It would be nice to somehow trap this error, so that when it occurs, I can automatically restart the sender, rather than forcing the user to. But, the problem is that I can't reproduce this error. More importatnly, I don't know specifically when it occurs. The users who are getting this tend to have a certain type of Novell service pack running on the same machine which I don't have. Therefore, I can't reproduce this error. Plus, it occurs randomly. I doesn't happen at any specific time.

So, my question is this: is there a way that I can trap this error, so that when it occurs, I can write some code to do something, rather than just having a big error jump out on the screen. And, if this is possible, where would I put this error trap in the existing code? Again, I don't know where the error occurs, so I wouldn't have a clue as to where to put this error trap. Is there someplace generic that I can place this so that no matter where/when this error occurs, this error trap can be called?
 
You might try something like this in the offending routine.

Sub YourRoutine

On Error GoTo HandleError

<you code here>

Exit Sub

HandleError:

if Err = 52 then
<Restart>
Resume
Else
MsgBox &quot;Error: &quot; & Err.Number & &quot;-&quot; & Err.Description
Resume Next
End If

End Sub

And of course you can expand this as your needs arise. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi CajunCenturion. Thanks for your response.

Do you happen to know an easy way to duplicate a runtime error, so that I can test this to make sure it works?

Also, where would this go in the code? My frmMain has a bunch of subs. Would I place this code just after the Option Explicit statement, or would it go somewhere else (like, within a Form_load subroutine). Does it even matter?

Option Explicit
Private Sub Form_Load()
LoadResStrings Me
Me.Left = GetSetting(App.Title, gstrRegKey, &quot;MainLeft&quot;, 1000)
Me.Top = GetSetting(App.Title, gstrRegKey, &quot;MainTop&quot;, 1000)
Me.Width = GetSetting(App.Title, gstrRegKey, &quot;MainWidth&quot;, 6500)
Me.Height = GetSetting(App.Title, gstrRegKey, &quot;MainHeight&quot;, 6500)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer

'Stop the Sender
mMain.StopSender
End Sub

Private Sub tmrTimer_Timer()
Dim blnFileIsHere As Boolean

'Check for valid TCP/IP parameters
If gstrRemoteHost = &quot;&quot; Or gintPort = 0 Then
Exit Sub
End If

'Check the state of DataFile object, if both are not closed then don't bother.
'However if there is no send or receive activity for a long time (60 sec.)
' then send the message
If gobjDataFile.State > 0 _
And fMainForm.tcpSender.State > 0 _
And (Date + Time - gdtmTimeOut) * 86400 < gIntConnRetry Then

Exit Sub
End If
End Sub
 
Add the following line into the offending routine

Err.Raise 52

Be sure to remove it once testing is complete :-D Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top