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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Error Trapping

Status
Not open for further replies.

Debeast

Programmer
Jul 18, 2002
28
GB
I know how Error Trapping works in a sub/function, but can you trap an error before it's returned to that sub funcion.

What im trying to do is build one error handler that functions for my entire project without the need to call it from every sub/function

IE :

Sub Deathtopotatoes ()

Dim Potatoe as Interger

Potatoe = 32 / 0 'divide by zero error

End Sub

Is there a GLOBAL way to grab the error ? without the need for code in the sub DeathtoPotatoes?

I know it's a hard one but you guys have impressed me inumerable times before

Another example of what im trying to do is :

Say i had 10 buttons on my form and each of these buttons calleda sub or two

i would have to implement error trapping in every buttton

This is what im trying to get around as i have over 200 buttons and b4 u ask no i'm not going to make them a control array
 
I don't use global error handling for a very specific reason. I want to know specifically in which sub/function the error occurred. Every sub that I have has an error handler, something like the following: (I can also turn off the error handler during development by setting the gBol_UseErrorHandler to false, then true when going into production)

Private Sub tabMain_Click(rInt_PreviousTab As Integer)

If (gBol_UseErrorHandler) Then
On Error GoTo HandleError
End If

<Handle the Tab Click>

Exit Sub

HandleError:

ErrMessage Trim(tabMain.Tab), (Me.Name & &quot;.tabMain_Click&quot;)
Resume Next

End Sub

In the ErrMessage global function, I know the actual name of the form and in this case, the event in which the error occurred. I can also pass in another parameter for more useful information - in the case, the ID of the tab that was clicked. All of the information is displayed to the user, and logged into a text file. So for functional, maintenance, and tracking reasons, I WANT the error handler in each and every routine.

I won't say anything about the 200 buttons turning into a control array for error handling reasons, but I would like to advise you that you have a limit of 256 controls. 200 buttons constitutes 200 controls. A control array of 200 buttons is 1 control. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
As far as I'm aware you can't do this. When an error is encountered VB works its way back through the pending procedures, if no error handler is found a default error message is returned. Therefor you would have to have an error handler in each procedure or a least in the calling procedures.

However I'm sure someone will correct me shortly!!
 
Is their an API which controls Errors?
If so how could it be used to trap ALL errors
 
Error trapping is one of the weaker areas of VB. There is no built-in way of doing global error handling - apart from the ability to bubble errors up that call stack to the first error handler it finds, and such bubbling stops the moment it hits any non-basic code in that stack. And the bubbling won't occur if no error handler installed in any of the routines in the stack. Oh, and buttons tend to be at the top of the stack...

 
There is an API called SetunhandledexceptionFilter

does anyone know how to use this my API programming is not very good
 
There is no global error handler, but there is an onError event on a form.
 
Yes, there is indeed an API call called SetunhandledexceptionFilter. It is designed to work with Structured Exception Handling, the necessary requirements of which are not built-in to VB. I wish you luck in trying to get it to work with VB (trust me, the effort involved will be far greater than adding a couple of lines to each of your subs...)

If you can't live without decent error handling in VB, I suggest you switch to VB.NET, which does support SEH.
 
Thanx for all your help guys

I'm bug gered if i can get the API to work :(
so i've bitten the bullet and just inserted code into EVERY sub

oh well

VB.net here i come
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top