Link to home
Start Free TrialLog in
Avatar of ginde
ginde

asked on

Error Handling

I have a VB5.0 application with lots of Modules. Unfortunately, many times it blows up in the night popping up a message and stops working. I would like to process the error message and continue.

1. Adding "On Error" statement in each and every procedure is too much of a work.
2. I don't want to disable error handling. I want to log the messages.

Is there any simple way of doing this?
Avatar of cognition
cognition

Error handling in VB is hierarchical. If there is no error handler in the current procedure then VB will look to the calling procedure. So you could put an error handler in a topmost calling procedure.

However you will not be able to resume, or resume next from the next line in the procedure where the error was generated, only a point in the procedure where the error handler is. This can work and you can continue processing.

You can buy error handling utilities that will do all the donkey work for you, adding error handling code. Search www.devx.com.
 

1. Not adding "On Error" statement is not a work
Error handling portion of your code should be 20-50 percent of your code.
You must learn how to handle misc errors.
Simple 'On error resume next' can prevent your app from crashing, but THERE IS NO SIMPLE way of doing real error handling.
Avatar of ginde

ASKER

I am sorry. I should have mentioned this already.  But, my application is somewhat multitasking application. What I mean is there are many timers and every 10-15 minutes these timer events looks for any incoming files from our different clients. And starts processing them if found.

In other words, it's not hierarchically executed process. Depending on different (timer) events these functions will be executed.

What I am really seeking for, is error handling on application level.

Cognition,
you  mentioned about some third party utilities. I am not much aware of their usage. Do you think, they have anything which might helpme do this? Do you have any good experience with those utilities?
Avatar of ginde

ASKER

Ameba,
I am just trying to cleanup the someone else's mess. And seeking a simplest solution.
Avatar of ginde

ASKER

Ameba,
I am just trying to cleanup the someone else's mess. And seeking a simplest solution.
If I remember good, the utility costs $1500. It uses a set of rules. This might help you for your situation, but you will have to check code anyway yourself. Again, it is not simple and it is very important.

Tell your employer that error handling can also be 20-50% of costs. Compare it with car manufacturers and security.

Of course you can reduce costs by using EE. Just post your function which needs error handling.
Cleaning up somebody elses mess is never any fun, and generaly more painful than writing the code yourself.  I don't think there is any way to avoid putting in an on error statement in every routine, however, to simplify things, have the error routine call public function, that way, you can copy and paste the code into every module.  Here is a function you can use to create a error log file.  I hope this helps at least a little.

Function ErrLogEntry(strUser As String, frmName As String, EventName As String, ErrCode As Integer, ErrDesc As String)
'Writes error information to a specified file
'If the log file is not found, a new one is created
Dim varRet
Dim strFileChk$, strLogFile$
Dim intFile%

On Error GoTo err_Handler

'Build string with directory and name of log file
strLogFile = App.Path & "\" & g_APPNAME & ".err"

'Get an unused file handle
intFile = FreeFile
'See if log file is out there
strFileChk = Dir$(strLogFile)

'Create new log file if not found
If Len(strFileChk) = 0 Then
varRet = MsgBox("Could not find log file: " & strLogFile & "." & Chr$(10) & Chr$(13), vbOKOnly + vbInformation, "Creating new log file.")
Open strLogFile For Output As #intFile
Print #intFile, "***** " & Now & " *****"
Print #intFile, "New log file started by " & strUser & "."
Print #intFile, ""
Close #intFile
End If

'Write error to log file
intFile = FreeFile
Open strLogFile For Append As #intFile
Print #intFile, "***** " & Now & " *****"
Print #intFile, "USER: " & strUser & " FORM: " & frmName & " EVENT: " & EventName & " ERROR: (" & ErrCode & ") " & ErrDesc
Print #intFile, ""
Close #intFile

Exit Function

err_Handler:
MsgBox "Error " & Err & ": " & Error$(Err) & " in ErrLogEntry function of " & g_APPNAME & " BAS file. Note: This error will not be written to the DOS Log File."
Exit Function

End Function
Why not use Voodoo Coder? It works great for adding in simple error handling code for you. The error messages will then give you options to click on and the line of code that is incorrect. It will add in the code and take it's code out.

http://www.voodoo-software.com/
Numega has a tool to add error handling and logging code (www.numega.com).

And I agree with Ameba, you REALLY need this amount of error handling in your code. But it makes debugging a pain in the ....
Well
u are not going to get out of
On error goto eh:
;;;
;;;
;;;
;;;
;;
;;;
eh:
what i did in one project is i created a error handler module then all other modules i declared the handler and then called my error handler module which had multiple case statements

if you want to create a type of log that is easy
in the errror handler module you declare a acces database create a log table in the database and write the information of the error to the log table (date , time , error num description ect)
or u could just write to a text documnet say called log.txt

hope i could help

Craig
you should use the app object
app.StartLogging logTarget:="Your log path and file", logMode:=0
in win95, it's to the specified filename, in winNT.. the event logger.

then make an exe that runs unattended to do further processing.

what i would do is run it in vb and wherever theres an error just write in some code.  i dont think your going to find a perfect answer to this.
Avatar of ginde

ASKER

Thats what I am doing for last few months. But that way I add more and more patches in the program and make it more and more error prone.
If U want to ensure that U can Resume Next, and U want logging, I can't imagine any solution simplier than writing a module to handle the logging which is identified in every single routine, i.e.,

sub abc()
on error goto xyz
end sub
xyz:
    Call Error Handler (abc)
    Resume Next
exit sub

Maybe there are third-party objects U can buy that will provide similar behavior, but I doubt  you will get exactly what U want.  Essentially U have identified one of the primary shortcomings of VB.  I'm sure U won't prefer this solution but from what I've read about error handling U just have to bite the bullet.

HTH



ASKER CERTIFIED SOLUTION
Avatar of freelnce
freelnce

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial