|
-
Dec 24th, 2006, 09:37 AM
#1
Thread Starter
Addicted Member
On Error
Is There AnyWay To Catch An Error On A Form Or A Module, And Write To A Text File Each Time It Occur
Like This:
VB Code:
On Error GoTo description:
description:
WriteFile(Err.Description & Function/Sub Name)
-
Dec 24th, 2006, 10:12 AM
#2
Addicted Member
Re: On Error
 Originally Posted by Vntalk
Is There AnyWay To Catch An Error On A Form Or A Module, And Write To A Text File Each Time It Occur
Like This:
VB Code:
On Error GoTo description:
description:
WriteFile(Err.Description & Function/Sub Name)
Yes you can do this. This is a commonly used tactic. What part of this do you have questions about?
An important concern is what to do with the error besides logging it. Once the error is logged you have to deal with the cause of the error. Also, you have to be very careful to preserve the information about the error so it is not lost while you are logging. Another thing is to make sure you are handling errors in your logging code, while at the same time not interferring with the operation of your applications main line of logic.
-
Dec 24th, 2006, 10:21 AM
#3
Re: On Error
You can use something like this (write in Append mode so you don't loose previous entries):
VB Code:
Option Explicit
Private Sub Form_Load()
On Error GoTo ErrorHandeler
Debug.Print 1 / 0
Exit Sub
ErrorHandeler:
writeLog Err.number, Err.description, "Form1 Form_Load"
End Sub
'in module
Public Sub writeLog(number As Long, description As String, procedure As String)
Dim ff As Integer: ff = FreeFile
Open App.Path & "\error.log" For Append As #ff
Print #ff, Format(Date, "dd.mm.yy") & ", " & Format(Time, "hh:mm:ss") & " - error #" & number & " - " & description & " - " & "in " & procedure & " procedure"
Close #ff
End Sub
-
Dec 24th, 2006, 10:32 AM
#4
Re: On Error
You must remember the most important point of an error trap.
Once an error occurs and you are in the error trap then no further error trapping will occur.
That means you don't write files and open files and do anything that can even remotely error out.
Proper method is:
Code:
On Error Goto Error_Routine
.
.
.
Exit Sub (or Exit Function)
DealWithError:
Write file - or whatever
Exit Sub
OtherDealWithError:
Do something else...
Exit Sub
Error_Routine:
If Err=.... Then Resume DealWithError
Resume OtherDealWithError
End Sub
-
Dec 24th, 2006, 11:30 AM
#5
Thread Starter
Addicted Member
Re: On Error
sorry if i'm not clear enought, but i want to know if we can catch a global error, not to trap an error on each sub or function, since there are many sub/function in a module or a form.
Global error trap to know which sub/function an error occur
-
Dec 24th, 2006, 11:32 AM
#6
Re: On Error
 Originally Posted by Vntalk
sorry if i'm not clear enought, but i want to know if we can catch a global error, not to trap an error on each sub or function, since there are many sub/function in a module or a form.
Global error trap to know which sub/function an error occur
Not sure but i don't believe it's possible.
-
Dec 24th, 2006, 11:46 AM
#7
Thread Starter
Addicted Member
Re: On Error
Also, i want to know everytime an error occur, my app just close without any error warning. A week a go , when an error occur it does pop up an error message
-
Dec 24th, 2006, 11:55 AM
#8
Re: On Error
Can you post your error trapping method (if there's any)?
-
Dec 24th, 2006, 11:56 AM
#9
Re: On Error
An error that occurs in a sub or func that is not trapped is simply passed along to the calling routine. If that calling routine has no error trap then it goes up the ladder to the next routine.
You should have an error trap in each and every routine - that's proper programming practice - actually have a standard template for each and every routine.
Here's what ours looks like:
Code:
'=====================================
'Module :basMain.bas
'Application :AMC
'Author :Author Name
'Creation Date :November 18, 2002
'=====================================
'Company Name...
'Address...
'City, State, Zip...
'=====================================
Public Sub RemoveTab(f As Form)
'Remove the Tab for a form when the Form closes
Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
Debug.Print " RemoveTab"
On Error GoTo Error_Handler
Begin:
With gfrmPrimary.tabForms
For x = 1 To .Tabs.Count
If CInt(.Tabs(x).Tag) = f.mintTabNo Then
.Tabs.Remove (x)
Exit For
End If
Next x
End With
Rtn_Caller:
Exit Sub
Error_Handler:
Call Fatal_Error(Err.Number, Err.Source, Err.Description, "RemoveTab")
Resume Rtn_Caller
End Sub
Fatal_Error is a routine that does something like this:
Code:
Public Function Fatal_Error(ByVal errNum As Double _
, ByVal errSource As String _
, Optional ByVal ErrDesc As String _
, Optional ByVal ErrRoutine As String _
, Optional ByVal booNoEnd As Boolean)
Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
Debug.Print "***Fatal_Error"
On Error GoTo Error_Handler
Begin:
s1 = ""
If Not booNoEnd Then s1 = " The AMC program will now close."
Select Case errNum
Case 52
gModal = True
MsgBox "Bad file name or number." & s1, vbCritical, "Bad File Name!"
gModal = False
Case 53
gModal = True
MsgBox "Can not find the INI file. Please make sure the INI file is at the same path as the executable and named correctly! The AMC program will now close", vbCritical, "INI File Not Found!"
gModal = False
.
.
.
-
Dec 24th, 2006, 01:23 PM
#10
Re: On Error
 Originally Posted by szlamany
...Once an error occurs and you are in the error trap then no further error trapping will occur...
That depends on how it is designed. Try the following to see what I mean:
VB Code:
Option Explicit
Private Sub Command1_Click()
On Error GoTo ErrHandler1
Debug.Print 1 / 0
Exit Sub
ErrHandler1:
Test
Err.Clear
MsgBox "Hello"
ErrHandler2:
MsgBox Err.Description
Exit Sub
End Sub
Public Sub Test()
On Local Error GoTo ErrHandler
Debug.Print 1 / 0
Exit Sub
ErrHandler:
MsgBox "Sub: Test; Err: " & Err.Description
Err.Clear
Resume Next
End Sub
-
Dec 24th, 2006, 02:06 PM
#11
Re: On Error
This is the point I was trying to make:
VB Code:
Option Explicit
Private Sub Command1_Click()
On Error GoTo ErrHandler1
Debug.Print 1 / 0
Exit Sub
ErrHandler1:
[b]MsgBox "In handler1 - lets make an error and see what happens"
Debug.Print 1 / 0[/b]
Test
Err.Clear
MsgBox "Hello"
ErrHandler2:
MsgBox Err.Description
Exit Sub
End Sub
Public Sub Test()
On Local Error GoTo ErrHandler
Debug.Print 1 / 0
Exit Sub
ErrHandler:
MsgBox "Sub: Test; Err: " & Err.Description
Err.Clear
Resume Next
End Sub
Last edited by szlamany; Dec 24th, 2006 at 03:06 PM.
Reason: Keep rb happy :)
-
Dec 24th, 2006, 03:00 PM
#12
Re: On Error
@szlamany: do you have problem with VBCODE tags - you're using CODE (reason why?).
-
Dec 24th, 2006, 03:05 PM
#13
Re: On Error
 Originally Posted by RhinoBull
@szlamany: do you have problem with VBCODE tags - you're using CODE (reason why?).
It's a silly habit from the DB section for the forum. If I put T-SQL in VBCODE tags sometimes the posters I'm working with think it's VB syntax. If they had SQLCODE tags I would be really happy!
At any rate, since I hand type the [code] and [/code] tags it's just a typing habit.
I'll try to keep on top of it
-
Dec 24th, 2006, 03:15 PM
#14
Re: On Error
Still VBCODE tags are better - they don't create annoying scrollable textbox.
-
Dec 24th, 2006, 03:24 PM
#15
Addicted Member
Re: On Error
 Originally Posted by Vntalk
sorry if i'm not clear enought, but i want to know if we can catch a global error, not to trap an error on each sub or function, since there are many sub/function in a module or a form.
Global error trap to know which sub/function an error occur
You cannot trap a "global" error - errors that are not trapped inside a sub, function, or property get or set are raised to the calling 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|