borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Query)Next Topic (visual basic 2005) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Related To Error Handling
Poster Message
chintu4u
Level: Graduate

Registered: 22-03-2006
Posts: 11

icon Related To Error Handling

Hi frd's is it possible to get in which sub or function and error has occured while using error handling.I m maintaining a Error Log in which wht's the Err.Number is and Err.Decription but it doesn't give in which particular sub or function of a particular form error has occured.As below code if user enters a specialization which is already exist in list then it will give error Specialization already exist and in Error Log it will note :
"08/25/06 12:28:22 PM -2147217873 Violation of PRIMARY KEY constraint 'PK_SPECIALIZATION_DETAILS'. Cannot insert duplicate key in object 'SPECIALIZATION_DETAILS'."
I also want tht know on which event or sub or function i.e over here on click of cmdsave_Click error has occured....

There is one thing tht in bellow line I shld write like this :
"Print #1, "cmdsave_click",Now, Err.Number, Err.Description"
which is not proper way of generalise coding, in each form in each event I have to write it.

***************************************************
Private Sub cmdsave_Click()
On Error GoTo Err:

If txtspecializationtype.text = "" Then
MsgBox "INSUFFICENT DATA" & vbCrLf & "FIELDS MARK WITH * ARE COMPULSORY", vbExclamation + vbOKOnly
Else
Call Removspc_UppCase(Me)
Call SaveClick_ED(Me)
adospecializatontype.Enabled = True
adospecializatontype.Recordset.Update
adospecializatontype.Refresh
Call FillList
MsgBox "YOUR RECORD HAS BEEN SAVED", vbInformation + vbOKOnly
Frame1.Enabled = False
adospecializatontype.Recordset.Requery
adospecializatontype.Refresh
adospecializatontype.Caption = "Total:" & adospecializatontype.Recordset.RecordCount
End If

Exit Sub
Err:
Open App.Path & "\ErrLog.txt" For Append As #1
Print #1, Now, Err.Number, Err.Description
Close #1
If Err.Number = -2147217873 Then
MsgBox "Specialization Type you are trying to save already exist", vbInformation + vbOKOnly, "Information"
Else
MsgBox "SOME Error HAS OCCURED", vbCritical + vbOKOnly, "Error"
End If
End Sub  
***************************************************

31-08-2006 at 10:11 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Related To Error Handling

Hi,
Try this:-
Create a new Code Module with the following procedures:

Option Compare Database
Public Const conStackSize = 10
Public strProcNames(1 To conStackSize) As String
Option Explicit

Public Sub PushDebugStack(strSubOrFunction)

Dim intX As Integer
    For intX = conStackSize To 2 Step -1
        strProcNames(intX) = strProcNames(intX - 1)
    Next intX
    strProcNames(1) = strSubOrFunction
End Sub

Public Sub PopDebugStack()

Dim intX As Integer
For intX = 1 To (conStackSize - 1)
        strProcNames(intX) = strProcNames(intX + 1)
Next intX
End Sub

Public Function fnPublicErrHandle(strMsg As String, blShowDebugStack As Boolean) As Boolean

Dim intX As Integer

If blShowDebugStack = True Then

strMsg = "An error occurred in the following procedure: " & strProcNames(1)

strMsg = strMsg & vbCrLf & "The following procedures were called: " & vbCrLf

For intX = 2 To conStackSize
    strMsg = strMsg & strProcNames(intX) & " ; "
Next intX

ElseIf strMsg = "" Then
strMsg = Err.Description

End If

MsgBox strMsg, vbInformation, "System Error"

End Function


Start each sub/function with a call to PushDebugStack, passing the name of the sub, and end it with a call to PopDebugStack. Then set your sub Error Handler to go to an Error Label that calls the Function fnPublicErrHandle.
If you pass an empty string for the Message with the blShowDebugStack flag set to False then you will just get the Error Object Description, otherwise it will  list all procedures that are running when the Error occurs.
Hope that helps.


____________________________
multi-tasking - the ability to hang more than one app. at the same time.

31-08-2006 at 06:33 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Related To Error Handling

Hello. One trick I use is a module-level/global string variable, like following:
' in an external module
Option Explicit

Public CurrentSubName As String

Then, when I write each routine, I start it by setting this variable, as
' in each module or form's module
Private Sub Form_Load()
    CurrentSubName = Me.Name & " - Form_Load"
    ' .... other code....
End Sub

Private Sub btnClickMe_Click()
    CurrentSubName = Me.Name & " - btnClickMe_Click"
    ' .... other code....
End Sub

Private Function MyFunction(arg As Variant) As Variant
    CurrentSubName = Me.Name & " - MyFunction(" & arg & ")"
    ' .... other code....
End Function

Private Sub Form_Unload(Cancel As Integer)
    CurrentSubName = Me.Name & " - Form_Unload(" & Cancel & ")"
    ' .... other code....
End Sub

' .... other Subs and Funx...
...and so on. I can omit to set it for non-critical routines, as
Private Sub cboMyCombo_Change()
    ' send the code to the Click event also when hitting keys in combo's text section
    cboMyCombo_Click
End Sub

Private Sub cboMyCombo_Click()
    CurrentSubName = Me.Name & " - cboMyCombo_Click/Change"
    ' .... other code....
End Sub


Each time the code stops for an error, I can read the variable value, either by a Debug.Print instruction in an error handler section, or by a ?CurrentSubName in the Debug window, or mousing over the variable into the code, or even writing it into a logfile and then reading it.
Var's value is still set on the name of the last routine it has got thru, so it's the name of the routine that raised the error.
When step-by-step debugging, I can even comment the initial routine setting, in order to know what other sub has called the error-generating one, and so on.

Hope it helps.

[Edited by yronium on 02-09-2006 at 05:52 PM GMT]

____________________________
Real Programmer can count up to 1024 on his fingers

02-09-2006 at 04:42 PM
View Profile Send Email to User Show All Posts | Quote Reply
chintu4u
Level: Graduate

Registered: 22-03-2006
Posts: 11
icon Re: Related To Error Handling

Thx yronium for u r reply but is there any other way were there would be a genral coding over here in each sub or function we have to write its name tht I was knowing can u help me out tht we hav to write in genral...and not like this :
CurrentSubName = Me.Name & " - Form_Load"

plz send me soon..
I will be thankfull to u.

02-09-2006 at 05:35 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Related To Error Handling

As far as I know, VB6 doesn't offer any native way to track the code flow. I heard there are some useful third-party VB IDE add-ons, but I don't know if any of them would match your request, and if it's free.

I don't know VB.Net, but I seem to remember it has something like a Parent Routine property that can help. But I'm quite sure it has nothing directly keeping the name of the routine that caused a particular error: I think it only provides some helpful properties on which you have to workaround with anyway. About it I leave the line open for somebody who knows it better.

Have a look on some demo versions of VB AddOns on download.com or snapfiles.com.

Hope it helps

____________________________
Real Programmer can count up to 1024 on his fingers

02-09-2006 at 06:14 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Related To Error Handling
Previous Topic (Query)Next Topic (visual basic 2005) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder