Results 1 to 8 of 8

Thread: Error Handling Made Simple

  1. #1

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Error Handling Made Simple

    One of the biggest problems programmers have is debugging their programs. Here are an examples of an error routine that will aid you in finding where in your code the actual problem arises when you have unexpected errors. There is also a routine that will show you how to handle expected errors.

    Unexpected Errors
    VB Code:
    1. Public Function YourFunction(...)
    2.  
    3. Dim ...
    4.  
    5.     On Error GoTo YourFunctionErrRtn
    6.         ...
    7.  
    8.     Exit Function
    9.  
    10. YourFunctionErrRtn:
    11.  
    12.     Stop
    13.     Resume Next
    14. End YourFunction

    This type of error function will allow you to enter the IDE debug mode (only if you are running in the IDE) on an error and then you can step through (f8) to the next line in your function directly after the line that caused the error. Now you know exactly where the error occurred. You can use tools such as ******* or MZTools to add this type of code to every function in your program or you may only want to add it to some function of your app if you know the problem areas.


    Expected Errors

    When you have expected errors you can handle the error in this fashion:
    VB Code:
    1. Public Function YourFunction (...)
    2.  
    3. Dim ...
    4.  
    5.     On Error Resume Next    ' Disable error handling
    6.  
    7.     Err.Clear
    8.     a = a/0         ' This can be any instruction that may cause an error
    9.  
    10.     If Err.Number <> 0 then ' This must come directly after the instuction you have that may cause an error
    11.         ' Handle Error
    12.     End If
    13.  
    14.     On Error GoTo 0
    15.  
    16. End YourFunction
    Last edited by randem; Oct 2nd, 2007 at 08:07 PM.

  2. #2

    Re: Error Handling Made Simple

    Err.Clear is a shorter way instead of setting Err.Number to 0

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Error Handling Made Simple

    Here are an examples of an error routine that will aid you in finding where in your code the actual problem arises when you have unexpected errors.
    Why not just set the "Break on All Errors" option.

  4. #4

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Error Handling Made Simple

    Quote Originally Posted by brucevde
    Why not just set the "Break on All Errors" option.
    Because some errors you might want to handle in the code as per my second example and some errors you just want to ignore (experienced programmers only). Break on all errors will annoy one if your code handles errors inside the code.

  5. #5

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Error Handling Made Simple

    Quote Originally Posted by Doomguy0505
    Err.Clear is a shorter way instead of setting Err.Number to 0
    That's a good point. I changed it.
    Last edited by randem; Oct 4th, 2006 at 11:59 PM.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling Made Simple

    Using "Stop" is a bad idea in my opinion, as it may not get removed before the code is compiled (especially if there is lots of code that would need to be checked).

    I would use "Debug.Assert False" instead, as it does the same job in the IDE, but has no effect when compiled.

  7. #7

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Error Handling Made Simple

    Actually Stop is a good idea. I did not include the conditional statement for debugging.

    IE.
    VB Code:
    1. If DebugFlag then
    2.      Stop
    3.      Resume Next
    4. End If

    I forgot where I was posting...

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling Made Simple

    Well that still takes more code than my suggestion, and requires the addition of a flag variable (which somebody may leave set to True in their compiled program), but it is a valid method.

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