Results 1 to 8 of 8

Thread: Error Handling Made Simple

Threaded View

  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.

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