Results 1 to 6 of 6

Thread: Error Trapping

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    London
    Posts
    23

    Error Trapping

    Does anybody know how to trap errors that could happen in all the object event procedures (textboxes, pictureboxes, listboxes, etc, etc) in a VB program? Obviously, I don't want to put On Error Goto lines in every single event procedure as I have got thousands of such procedures!

    Kind Regards, markunosquirrel
    Regards, Mark.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Error Trapping

    Originally posted by markunosquirrel
    Obviously, I don't want to put On Error Goto lines in every single event procedure as I have got thousands of such procedures!
    better get coding

    by any professional standard, you should error trap every routine, other than routines that you know NO POSSIBLE error could occur. For example, I have a routine that converts state abbreviations to the full state name NJ = New Jersey

    its a select case routine and there really is no possible way for it to error out, and it has a case else at the end incase a bad abbreviation is passed to it... but any other routine that COULD cause an error, should have an error handle

    what you may want to do is write a routine to handle errors and call it from each sub if there is an error

    something like below

    VB Code:
    1. Private Sub MyRoutine()
    2.  
    3. On Error Goto EH:
    4.  
    5.     'blah blah blah code here
    6.  
    7.     Exit Sub
    8.  
    9. EH:
    10.     Call HandleError(Err.Number, Err.Description)
    11. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    London
    Posts
    23
    What I'm after is some way of trapping any errors that the program generates that I haven't already trapped. I need to be able to write something like...

    On Any Untrapped Error at all in the program Goto Stop_Crash_Error_Handler

    The program is very big indeed and being human I may not have written error trapping code when I should have done. I can't look through zillions of lines of code checking it.

    The problem that I'm trying to solve is "How can I guarantee that the program doesn't just crash but exits in a tidy manner?"

    Regards, Markunosquirrel
    Regards, Mark.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    the only way to do what you want is what i just suggested... VB has absolutly no way of having a "GLOBAL" error handle to just accept any error that generates.

    When an error occurs the error will travel all the way back up the call stack until an error handler is encountered, if none is then you get the runtime error.

    For example, if you have a routine called MySub1 and in this routine you call MySub2. Now lets say MySub1 has an error handler, but MySub2 does not. If an error occurs in MySub2, the error will be sent back to MySub1 and will try to get handled there depeneding on what your error handler code is.

    So theoretically... the only way you could have a global error handler is if your entire program was run under 1 routine...

    Just a side thought.. perhaps if you had a sub main in a module to start your app, and that had an error handler in it, and you call your main form modally from the sub main, any error that occurs MAY go back to the sub main's error handler.. but I haven't tested that... worth a shot though

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    London
    Posts
    23
    Many thanks to kleinma for the help. I suspected as much but I will try your suggestion of an On goto in Sub Main. Once again, Many thanks,

    Regards, Markunosquirrel.
    Regards, Mark.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    If kleinma's suggestion doesn't work for you and you decide to put error handling in most if not all of your routines, I suggest you download MZ-Tools. It is an excellent IDE addin that among many other things allows to add with one click something like the following, even in routines with existing code.
    VB Code:
    1. Public Sub SomeProcedure()
    2. '***************************************************************************
    3. 'Purpose:
    4. 'Inputs :
    5. 'Outputs:
    6. '***************************************************************************
    7.  
    8.     On Error GoTo ErrorRoutine
    9.  
    10.    
    11.  
    12.     Exit Sub
    13.  
    14. ErrorRoutine:
    15.  
    16.     DisplayError "SomeProcedure"
    17.  
    18. End Sub
    That's what I do but the format is totally variable. BTW my DisplayError routine looks like the following.

    VB Code:
    1. Public Sub DisplayError(strRoutine As String)
    2. '***************************************************************************
    3. 'Purpose: Common error display routine
    4. 'Inputs:  strRoutine - The name of the routine where the error occurred
    5. 'Outputs: None
    6. '***************************************************************************
    7.  
    8.     MsgBox ResolveResString(resDisplayError, _
    9.            "|1", str$(Err.Number), _
    10.            "|2", Err.Source, _
    11.            "|3", strRoutine & "." & vbCrLf & vbCrLf, _
    12.            "|4", Err.Description), vbExclamation, , Err.HelpFile, Err.HelpContext
    13.    
    14. End Sub

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