Results 1 to 6 of 6

Thread: Print error to a txt file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Print error to a txt file?

    The very basic errorhandler is something like this

    vb Code:
    1. Public Sub Something()
    2.  
    3. On Error Goto ErrorHandler
    4.  
    5. 'DoSomething
    6. 'DoSomethingElse
    7.  
    8. ErrorHandler:
    9. FileNumber = FreeFile
    10. Open "Error.log" For Output As #FileNumber
    11. Print#FileNum, "Error in Function Something: " & Err.Number & " - " & Err.Description
    12. Close#FileNum
    13. 'Resume or Exit Sub or blame Microsoft or whatever else
    14. End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Print error to a txt file?

    Quote Originally Posted by JamieWarren09 View Post
    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:
    1. 'In a public Module
    2. Public Sub ErrorHandler(ByRef FunctionName As String, ByVal, ErrNumber As Long, ByRef ErrDescription As String)
    3. Dim FileNumber As Integer
    4.  
    5. FileNumber = FreeFile
    6. Open MyExePath & "\error.log" For Append As #FileNumber
    7. Print#FileNumber, "Date/Time of Error: " & Format(Now,"YYYY.MM.DD HH:MM:SS")
    8. Print#FileNumber, "Error-Code: " & ErrNumber & " - Error-Description: " & ErrDescription
    9. Print#FileNumber, "In Function: " & FunctionName
    10. Close#FileNumber
    11. 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:
    1. Private Sub MySensitiveCodeSub()
    2. 'Dim some Variables here
    3. On Error Resume Next    'DON'T START ON ME ABOUT THIS!
    4. 'Execute sensitive Code here
    5. If Err.Number<>0 then   'Err.Number = 0 means: No Error occured
    6.  
    7.     Call ErrorHandler("MySensitiveCodeSub", Err.Number, Err.Description)
    8.     Err.Clear
    9.     'Do something else, like Exit the Sub, give a message to the User, continue with your code, whatever else
    10.    
    11. End If
    12. 'Execute other sensitive stuff
    13.  
    14. 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
  •  



Click Here to Expand Forum to Full Width