CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    24

    how to trap an error

    hi need some help with this i dont know how to trap errors properly

    i have a module passing code from my listbox when user double clicks it (it starts a program using shell function)

    now if the file is not found i need to trap the error (err.number is 53 "File Not Found")

    when the error is raised i need a msgbox to show stating game is not found and then i need the error to be cleared so that the user can select another line in the listbox.

    now the code in the module has 52 shell statments depending on which line in the listbox is double clicked.

    below is the code that i need to trap errors on.

    Code:
    Public Sub Load_Game_Paragon()
    
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 0 Then
        Shell App.Path & "\Roms\Paragon\AroundTheWorld\AroundTheWorld.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 1 Then
        Shell App.Path & "\Roms\Paragon\Battleship\Battleship.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 2 Then
        Shell App.Path & "\Roms\Paragon\BillyTheQuid\billy_the_quid.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 3 Then
        Shell App.Path & "\Roms\Paragon\Blackjack\BlackJack.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 4 Then
        Shell App.Path & "\Roms\Paragon\Blockbusters\Blockbusters.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 5 Then
        Shell App.Path & "\Roms\Paragon\BrainTeaser\BrainTeaser.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 6 Then
        Shell App.Path & "\Roms\Paragon\BuckShot\buckshot.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 7 Then
        Shell App.Path & "\Roms\Paragon\Bullseye\BullsEye.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 8 Then
        Shell App.Path & "\Roms\Paragon\BullseyeTV\BullsEyeTV.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 9 Then
        Shell App.Path & "\Roms\Paragon\Capoeria\capoeria.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 10 Then
        Shell App.Path & "\Roms\Paragon\CashPhrase\game.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 11 Then
        Shell App.Path & "\Roms\Paragon\Chainetters\Chainletters.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 12 Then
        Shell App.Path & "\Roms\Paragon\Chockablock\Chockablock\Chockablock.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 13 Then
        Shell App.Path & "\Roms\Paragon\CopsNRobbers\CopsNRobbers.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 14 Then
        Shell App.Path & "\Roms\Paragon\DungeonDosh\DungeonDosh.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 15 Then
        Shell App.Path & "\Roms\Paragon\Finders\Finders\Find&Seek.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 16 Then
        Shell App.Path & "\Roms\Paragon\FruitFrenzy\FruitFrenzy.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 17 Then
        Shell App.Path & "\Roms\Paragon\GoldenBoot\GoldenBoot.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 18 Then
        Shell App.Path & "\Roms\Paragon\GoldenShot\GoldenShot-LF.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 19 Then
        Shell App.Path & "\Roms\Paragon\GoldRush\GoldRush.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 20 Then
        Shell App.Path & "\Roms\Paragon\GuessWho\GuessWho.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 21 Then
        Shell App.Path & "\Roms\Paragon\HangmansHauntedHouse\HauntedHouse-LF-Static.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 22 Then
        Shell App.Path & "\Roms\Paragon\HexAppeal\HexAppeal.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 23 Then
        Shell App.Path & "\Roms\Paragon\HocusPocus\hocuspocus.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 24 Then
        Shell App.Path & "\Roms\Paragon\InternationalGolfPro2\golf2.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 25 Then
        Shell App.Path & "\Roms\Paragon\LordOfTheBling\LordOfTheBling.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 26 Then
        Shell App.Path & "\Roms\Paragon\Matrix\Matrix.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 27 Then
        Shell App.Path & "\Roms\Paragon\Moneycomb\MONEYCOMB.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 28 Then
        Shell App.Path & "\Roms\Paragon\Ninelives\NineLives.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 29 Then
        Shell App.Path & "\Roms\Paragon\PharaohsTreasure\PharaohsTreasure.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 30 Then
        Shell App.Path & "\Roms\Paragon\PhilTaylorWorldChampionshipDarts\Darts.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 31 Then
        Shell App.Path & "\Roms\Paragon\PimpMyRide\PimpMyRide.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 32 Then
        Shell App.Path & "\Roms\Paragon\PocketMoney\pool.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 33 Then
        Shell App.Path & "\Roms\Paragon\ShootForTheStars\ShootForTheStars.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 34 Then
        Shell App.Path & "\Roms\Paragon\SkillBallBingo\game.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 35 Then
        Shell App.Path & "\Roms\Paragon\SouthPark\South Park.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 36 Then
        Shell App.Path & "\Roms\Paragon\SouthPark2\SouthPark.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 37 Then
        Shell App.Path & "\Roms\Paragon\SpaceQuest\SpaceQuest.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 38 Then
        Shell App.Path & "\Roms\Paragon\Spin2Win\Spin2Win.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 39 Then
        Shell App.Path & "\Roms\Paragon\SpotDeluxe\SpotTheDifference3-LF.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 40 Then
        Shell App.Path & "\Roms\Paragon\SpotTheBall\SpotTheBall.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 41 Then
        Shell App.Path & "\Roms\Paragon\SpotTheDifference2\SpotTheDifference2-LF.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 42 Then
        Shell App.Path & "\Roms\Paragon\StrikeBowling\StrikeBowling.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 43 Then
        Shell App.Path & "\Roms\Paragon\StrikeItRich\StrikeItRich.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 44 Then
        Shell App.Path & "\Roms\Paragon\StrikeZone\StrikeZone.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 45 Then
        Shell App.Path & "\Roms\Paragon\Sudoku\Sudoku.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 46 Then
        Shell App.Path & "\Roms\Paragon\SumUp\SumUp_1024.exe"
        Form1.WindowsMediaPlayer1.URL = ""
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 47 Then
        Shell App.Path & "\Roms\Paragon\TheUltimateMovieQuiz\MovieQuiz4L.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 48 Then
        Shell App.Path & "\Roms\Paragon\TOOT\TOOT.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 49 Then
        Shell App.Path & "\Roms\Paragon\TrivialPursuit\TrivialPursuit.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 50 Then
        Shell App.Path & "\Roms\Paragon\TrivialPursuit2\TrivialPursuit-LF.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 51 Then
        Shell App.Path & "\Roms\Paragon\WodCube\GWHL_WordCube.exe"
    End If
    If Form1.lblBtnPress.Caption = "4" And Form1.List1.ListIndex = 52 Then
        Shell App.Path & "\Roms\Paragon\WordUp\WordDrop.exe"
    End If
    
    End Sub
    there will be another 200 lines of code in different subs in the module i would like the error code to be used on.

    thanks for any help

  2. #2
    Join Date
    Sep 2001
    Posts
    160

    Re: how to trap an error

    Try putting this in you sub or function
    Code:
       
    
        Dim int_ErrReturn           As Integer
        Dim str_ProcName            As String
        
        str_ProcName = "Your sub/function name"
        On Error GoTo ProcError
    
    '
    
    Code goes between here
    
    '
        
        On Error Resume Next
        
               
        Exit Function ' you use sub instead of function for subs
        
    ProcError:
        
        g_bln_ErrEdit = False
        g_str_ErrProcName = str_ProcName
        g_lng_Err = Err
        g_str_Error = Error
        g_bln_ErrCancel = False
        
        int_ErrReturn = g_f_int_ErrorRoutine(0)
    
        Resume Next
    Then put this code in a global module

    Code:
    Public Function g_f_int_ErrorRoutine(int_ErrNo As Integer) As Integer
    
        
        Dim str_Time                            As String
        Dim str_Date                            As Date
        
    
    
        str_Time = Time
        str_Date = Date
        
        
        ' If it is a program error and not a system error
        ' Then set error message and error title
        
        If int_ErrNo Then
       
            Select Case int_ErrNo
            
                Case 1: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                        
                Case 2: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                        
                Case 3: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                        
                Case 4: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                        
                Case 5: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                
                Case 6: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                
                Case 7: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                
                Case 8: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                        
                Case 9: g_str_ErrMsg = "  "
                        g_str_ErrTitle = "  "
                
                Case 10: g_str_ErrMsg = "You Have Ended This Application !" & vbNewLine & vbNewLine & _
                                        " Do You Want To Save The Current Information ? (Y/N) " & vbNewLine & vbNewLine & _
                                        " Press Cancel To Return To The Application . "
                         g_str_ErrTitle = g_str_FormName
                
                Case 11: g_str_ErrMsg = " "
                        g_str_ErrTitle = "  "
                        
                Case 12: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                        
                Case 13: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                
                Case 14: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                        
                Case 15: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                        
                Case 16: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                        
                Case 17: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
                        
                Case 18: g_str_ErrMsg = "  "
                         g_str_ErrTitle = "  "
            End Select
            
        End If
        
        ' Change button(s) to yes/no if g_str_ErrMsg = 4
        
        If int_ErrNo Then
        
            Select Case int_ErrNo
            
                Case 4: g_int_ErrButton = 36
                
                Case 9: g_int_ErrButton = 35
                
                Case 10: g_int_ErrButton = 35
                
                Case Else: g_int_ErrButton = 16
                
            End Select
            
        End If
        
        
        ' If program error then display message box and get return
        
        If int_ErrNo Then
        
            g_f_int_ErrorRoutine = MsgBox(g_str_ErrMsg, g_int_ErrButton, g_str_ErrTitle)
            
            g_bln_ErrEdit = False
            
            Open g_str_ErrorLog For Append As #1
            
            Write #1, str_Time, str_Date, g_str_ErrTitle, g_str_ErrMsg, g_int_ErrButton, g_f_int_ErrorRoutine, vbCrLf & vbCrLf
            
            Close #1
    
            
            Exit Function
        
        End If
        
        
        ' If system error then display message box and get return
            
        g_str_ErrTitle = "Operating / VB System Error in " & _
                             g_str_ErrProcName
                             
        g_str_ErrMsg = "Error # " & _
                        Trim$(Str$(g_lng_Err)) & _
                        " - " & _
                        g_str_Error & vbCrLf & vbCrLf
                           
        If g_bln_ErrCancel Then
            g_str_ErrMsg = g_str_ErrMsg & _
                            "This Program will Now Terminate"
                            g_int_ErrButton = 16
        Else
            g_str_ErrMsg = g_str_ErrMsg & _
                            "Cancel Program ( Yes, No )"
                            g_int_ErrButton = 20
        End If
            
        g_f_int_ErrorRoutine = MsgBox(g_str_ErrMsg, g_int_ErrButton, g_str_ErrTitle)
        
    '    Open g_str_ErrorLog For Append As #1
    '
    '    Write #1, str_Time, str_Date, g_str_ErrTitle, g_str_ErrMsg, g_int_ErrButton, g_f_int_ErrorRoutine, vbCrLf & vbCrLf
    '
    '    Close #1
            
    
        
        If Not g_bln_ErrEdit Then
            If g_bln_ErrCancel Then
                End
            Else
                If g_f_int_ErrorRoutine = 6 Then
                    End
                End If
            End If
        End If
        
    End Function
    Be sure to declare the data fields in the app. There may be more than you need in here but this should help.

  3. #3
    Join Date
    Apr 2009
    Posts
    394

    Re: how to trap an error

    Simple, easy, direct, and proper way to catch an error is not to use on error resume next or on error goto 0... For example, you just created the following sub...
    Code:
    Private Sub MySub()
    
    End Sub
    and now to add error trapping to it...
    Code:
    Private Sub MySub()
    
    On Error GoTo LabelName 'now label name can be almost anything but here I normally copy the subs name
    'and use it. So my label name would be MySubError
    
    'exit the sub so the error handler is not invoked
    Exit Sub
    'now this next line is the label, or as I call it the anchor. If an error occures, the program will jump
    'to this point and execute the next line
    MySubError:
    
    'and so you can check for that error
    If Err.Number = 53 Then
      MsgBox "File Not Found! Please Try Again.", vbOkOnly + vbInformation, "Not Found"
      Err.Clear
    Else
      MsgBox "My Sub " & Err.Number & ":" & Err.Description
    End If
    
    End Sub


    Good Luck

  4. #4
    Join Date
    Sep 2001
    Posts
    160

    Re: how to trap an error

    "Simple, easy, direct, and proper way to catch an error is not to use on error resume next or on error goto 0... For example, you just created the following sub... "
    vb5prgrmr is correct about usning on error resume next, I did not notice that I pasted a bit of code that contained that. There was instance when I wanted to use this such as ...

    Code:
    On Error Resume Next
            
            m_obj_rst.MoveLast
            m_obj_rst.MoveFirst
            
    On Error GoTo ProcError

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured