|
-
Sep 6th, 2012, 04:25 AM
#1
Thread Starter
Fanatic Member
Print error to a txt file?
Hi guys, For some reason when my app is a .exe file it randomly stops responding and crashes every now and then but does not display why. does anyone know how I can make it print the reason why it crashed into a text file?
Thanks
Jamie.
-
Sep 6th, 2012, 04:34 AM
#2
Re: Print error to a txt file?
Only if you're tracing your code with error-handlers.
Then it's pretty easy to implement an error-log.
If you don't have errorhandlers, then your only option would be the event-log of Windows.
-
Sep 6th, 2012, 04:40 AM
#3
Thread Starter
Fanatic Member
Re: Print error to a txt file?
Thanks, How do I add error-handlers? Basically the team just want to be able to see which errors are causing the app to crash, so we can fix them.
Ta
-
Sep 6th, 2012, 05:10 AM
#4
Re: Print error to a txt file?
The very basic errorhandler is something like this
vb Code:
Public Sub Something()
On Error Goto ErrorHandler
'DoSomething
'DoSomethingElse
ErrorHandler:
FileNumber = FreeFile
Open "Error.log" For Output As #FileNumber
Print#FileNum, "Error in Function Something: " & Err.Number & " - " & Err.Description
Close#FileNum
'Resume or Exit Sub or blame Microsoft or whatever else
End Sub
-
Sep 6th, 2012, 05:45 AM
#5
Thread Starter
Fanatic Member
Re: Print error to a txt file?
Fantastic, Where would be the best place to put that code? I mean will i have to put it on every public sub to catch an error? or is it possible to have it in one general place?
thanks
-
Sep 6th, 2012, 06:26 AM
#6
Re: Print error to a txt file?
 Originally Posted by JamieWarren09
Fantastic, Where would be the best place to put that code? I mean will i have to put it on every public sub to catch an error? or is it possible to have it in one general place?
thanks
If you pose this question to 5 programmers, you will get 5 different answers 
I'm doing it like this:
vb Code:
'In a public Module Public Sub ErrorHandler(ByRef FunctionName As String, ByVal, ErrNumber As Long, ByRef ErrDescription As String) Dim FileNumber As Integer FileNumber = FreeFile Open MyExePath & "\error.log" For Append As #FileNumber Print#FileNumber, "Date/Time of Error: " & Format(Now,"YYYY.MM.DD HH:MM:SS") Print#FileNumber, "Error-Code: " & ErrNumber & " - Error-Description: " & ErrDescription Print#FileNumber, "In Function: " & FunctionName Close#FileNumber End Sub
Then in the functions/sub, that contain sensitive code (code which can crash, like saving something to a database, or validating User-Input)
vb Code:
Private Sub MySensitiveCodeSub() 'Dim some Variables here On Error Resume Next 'DON'T START ON ME ABOUT THIS! 'Execute sensitive Code here If Err.Number<>0 then 'Err.Number = 0 means: No Error occured Call ErrorHandler("MySensitiveCodeSub", Err.Number, Err.Description) Err.Clear 'Do something else, like Exit the Sub, give a message to the User, continue with your code, whatever else End If 'Execute other sensitive stuff End Sub
You could even improve my ErrorHandler-Function with a ParamArray Argument to send information about the values of the variables and so on.
Last edited by Zvoni; Sep 6th, 2012 at 10:06 AM.
Reason: I forgot Err.Clear
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
|