CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Dec 2008
Posts
24

## error handling

hi heres another question

how to i catch an error and then skip the piece of code causing the error

i have a list box with 30 odd items in it and they will open a individual app on double click.

now most of the items wont have a exe in the specified folder yet to run and when i run my app and double click a item i get either error 53 file not found.

now i want to catch that error before the error msg is displayed and have a label display the text "Game Not Available"

how do i do this thanks

2. ## Re: error handling

Not too hard. I added a few lines to my old sample!

Code:
Option Explicit

Dim x As Integer, st As String, y As Integer
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
On Error GoTo trapit
Open App.Path & "\to do3.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
Dim words() As String
For x = 0 To UBound(str)
words = Split(str(x)) ' Split into WORDS
For y = 0 To UBound(words)
st = st & str(x) & vbCrLf & vbCrLf ' one line for each word
Next y
Next x
MsgBox st
Exit Sub
trapit:
If Err.Number = 53 Then
MsgBox ("Didn't work")
End If
End Sub

3. Junior Member
Join Date
Dec 2008
Posts
24

## Re: error handling

not too sure how to use your code in my app yet gonna have a good look at it in the morning.

thanks again

4. Super Moderator Power Poster
Join Date
Jul 2008
Location
WV
Posts
5,337

## Re: error handling

Any line that follows the on error statement will cause the code to jump directly to the trapit: line if an error occurs effectively skipping the code between the source of the error and the end of the sub routine. Make sure that you use the exit sub or exit function just above your trap label so as to insure it only executes when an error has occurred.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•