dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: error handling

  1. #1
    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. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: error handling

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

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2008
    Posts
    24

    Re: error handling

    thanks for the reply

    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. #4
    DataMiser is offline 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)