Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling

Status
Not open for further replies.

bomayed

Technical User
May 13, 2002
101
0
0
AE
I know how to use:

ON ERROR GOTO LINE
DO SOMETHING
LINE:
DEAL WITH ERROR


But this works in the current sub or function. What if I want to use ( ON ERROR GOTO LINE ) to watch errors anywhere in the whole project for all the forms in the project ? is there like a global error handling ? how can it be done?

Thanks

 
From what I know, you can't do it in the way yow put it.
But you can have a public function/sub in your project that deals errors. You just have to call it from each of your functions/subs that you want to trap errors in.
For example, instead of
Code:
LINE:
DEAL WITH ERROR
you can write
Code:
LINE:
Call MyErrorHandlingRoutine(Param1, Param2, ...ParamN)
where MyErrorHandlingRoutine is your function/sub dedicated to error handling at project level.

Hope I've been helpful,
Bogdan Muresan.
 
SO YOU MEAN I HAVE TO PUT IN EACH FUNCTION/ SUB THIS CODE:

ON ERROR GOTO LINE

DO SOMETHING HERE


LINE:
CALL MY_PUBLIC_FUNCTION()

I WISH THERE IS A WAY WHERE I DON'T HAVE TO DO THIS IN ALL MY SUBS OR FUNCTIONS . . . :( = = =


THANKS BOGDANMBM
 
You can put a on error goto at the start of your program (in your sub main, etc). The only catch is that any error in the call stack ends up back there (which may not be what you want).

So most VB programmers put on error gotos in every method. The good thing about doing this is you can end up with a stack trace by looking at all the errors in your errorlog (very nice, so you can see how you got to the method with the error). The bad thing (as you say) is that you have to do the work to put them in all your methods.

Something that makes it easier is a free tool called MZTools. It has the ability to store an error logging template, and once you have it set up, it only takes a mouse click to add it to a method you're writing.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
>>I WISH THERE IS A WAY WHERE I DON'T HAVE TO DO THIS IN ALL MY SUBS OR FUNCTIONS

Although you can not do exactly what you are wanting, error handling will go back up the program stack the most recent sub that implemented and On Error statement. If there is non implmented, then VB handles the error itself, ultimately ending execution of the program. So, you don't necessarily have to have on in every sub... just in subs that are called from form events... make sense?
 
>I WISH THERE IS A WAY

Yep, VB programmers have been crying out for better error handling features for years. We eventually got them in VB.NET...
 
find out about MZTools, it lets you add with a shortcut key or a mouse-click your custom-defined error-handling code to each procedure. So in stead of typing in for each procedure:

Code:
On Error Goto Err_Handler

  'Procedure code goes here

ExitProcedure:
  On Error Goto 0
  Exit Sub/Function

Err_Handler:
  dsSemReport dfSemCreate({module_name}, {procedure_name}, err.number, err.description)
  goto ExitProcedure

you would simply click the 'add error-handling code' button on your MZTools toolbar, and it puts whatever text you defined as your error-code in the current procedure. Never mind what I use for error-handling, you could put anything you want there. MZTools also provides variables like {module_name} and {procedure_name} and {procedure_type} to use in your code. In the example above, you could replace the "Exit Sub/Function" with "Exit {PROCEDURE_TYPE}" and it will automatically add the correct exit parameter.

This saves on typing and helps to keep your error-handling code consistent throughout your application.


__________________
code is enduring
 
My MZTools template looks like this:
Code:
    Dim lErrNum As Long
    Dim sErrMsg As String

    On Error GoTo {PROCEDURE_NAME}_ErrHandler
    {PROCEDURE_NAME} = False  'assume error
    
    {PROCEDURE_BODY}

    {PROCEDURE_NAME} = True
    GoTo {PROCEDURE_NAME}_Cleanup
    
{PROCEDURE_NAME}_ErrHandler:
    lErrNum = Err.Number
    sErrMsg = Err.Description
    Call LogError(m_sMODULENAME, "{PROCEDURE_NAME}", lErrNum, sErrMsg, _
                    EVENT_TYPE_ERROR, Erl, "")
    ' Fall through into cleanup

{PROCEDURE_NAME}_Cleanup:
Where LogError is our common error logging method, and m_sMODULENAME is a constant we define at the top of every code module.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top