Solved

Error Handling

Posted on 1999-01-06
15
281 Views
Last Modified: 2010-05-03
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?
0
Comment
Question by:ginde
15 Comments
 
LVL 3

Expert Comment

by:cognition
ID: 1454374
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.
 

0
 
LVL 15

Expert Comment

by:ameba
ID: 1454375
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.
0
 
LVL 2

Author Comment

by:ginde
ID: 1454376
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?
0
 
LVL 2

Author Comment

by:ginde
ID: 1454377
Ameba,
I am just trying to cleanup the someone else's mess. And seeking a simplest solution.
0
 
LVL 2

Author Comment

by:ginde
ID: 1454378
Ameba,
I am just trying to cleanup the someone else's mess. And seeking a simplest solution.
0
 
LVL 15

Expert Comment

by:ameba
ID: 1454379
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.
0
 
LVL 3

Expert Comment

by:jjmartin
ID: 1454380
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:scully12
ID: 1454381
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/
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1454382
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 ....
0
 
LVL 4

Expert Comment

by:CraigLazar
ID: 1454383
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
0
 

Expert Comment

by:lmartin080698
ID: 1454384
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.

0
 

Expert Comment

by:edunc
ID: 1454385
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.
0
 
LVL 2

Author Comment

by:ginde
ID: 1454386
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.
0
 

Expert Comment

by:Billaban
ID: 1454387
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



0
 

Accepted Solution

by:
freelnce earned 50 total points
ID: 1454388
Unfortunately, VB error trapping is not automatic enabled and added for your code (unless you want to count the always fatal "Run-time Error ..." message). It is up to you to provide the code that performs the trapping and handling of any errors. It is always something that most developers either leave for last-minute or don't do at all. A fundamental rule is that error trapping should be placed in ANY routine that performs any type of I/O or computation as this is the most likely areas to produce errors. If laziness is your desire then you might wish to write an app to work through your code and automatically insert error trapping into it. Your right, it is a lot of work, but it is also very necessary work for a reliable application. The code below shows this practice in full form. I hope it helps.

Option Explicit


'--- You will have to place the following code into every procedure
'    in your application that performs ANY type of I/O. This ensures
'    that the errors are handled in the necessary manner. You also
'    need to remember that even though you can set nested error traps
'    you cannot protect against nested errors. If another error occurs
'    in your application while a current error condition is in effect
'    then the application will crash with a run-time error.
'
'    This code produces a flat ASCII file that contains the error log.

Public bLogError As Boolean


Private Sub Form_Load()
  '--- Turn error logging on.
  bLogError = True
End Sub

Public Sub AnySubOrFunction()
Dim sMsg        As String

Const sProcName As String = "Error ModuleOrClassName.AnySubOrFunction()"

  On Error GoTo Error_AnySubOrFunction
 
  '--- Place your code between these lines.
 
 
  '--- End of your code.


Exit_AnySubOrFunction:
  On Error GoTo 0
  Exit Sub
 
Call_LogError:
  Call LogError
  GoTo Exit_AnySubOrFunction

Error_AnySubOrFunction:
  sMsg = "Error " & CStr(Err) & vbCr
 
    Select Case Err
      Case 0  '--- Place specific error number here.
     
        '--- Use this if there is a particular error that you want
        '    the procedure to automatically recover from and continue
        '    with is processing.
       
      Case Else
        sMsg = sMsg & Err.Description
    End Select
   
    If bLogError Then
      '--- We do it this way because of the nested error problem
      '    I explained above. If the LogError routine produced an
      '    error then the application would crash because there
      '    is a current unresolved error condition.
      Resume Call_LogError
     
    Else
      MsgBox sMsg, vbInformation, sProcName
    End If
   
  Resume Exit_AnySubOrFunction
End Sub

Public Function LogError(ByRef sError As String) As Boolean
Dim iSrc        As Integer
Dim sLogFile    As String
Dim sMsg        As String

Const sProcName As String = "Error ModuleOrClassName.LogError()"

  On Error GoTo Error_LogError
 
  sLogFile = App.Path & "\Error.Log"
  iSrc = FreeFile
 
    '--- Open, write and then close the error log file.
    Open sLogFile For Append As #iSrc
      Print #iSrc, TimeStamp
      Print #iSrc, sError
    Close #iSrc

  '--- Return True to show that log activity has taken place or
  '    to show success.
  LogError = True
 
 
Exit_LogError:
  On Error GoTo 0
  Exit Function
 
Error_LogError:
  sMsg = "Error " & CStr(Err) & vbCr
 
    Select Case Err
      Case 0  '--- Place specific error number here.
     
        '--- Use this if there is a particular error that you want
        '    the procedure to automatically recover from and continue
        '    with is processing.
       
        '--- You can then resume in whatever manner required.
        'Resume
        'Resume Next
        'Resume SpecificLabel
           
           
      Case Else   '--- Default action.
        '--- You can ignore any errors that occur here just by
        '    commenting out the MsgBox line below.
        sMsg = sMsg & Err.Description
        MsgBox sMsg, vbInformation, sProcName
    End Select
   
  Resume Exit_LogError
End Function

Public Function TimeStamp() As String
Dim sMsg As String

  TimeStamp = vbCrLf & _
              String$(40, "=") & vbCrLf & _
              Format$(Now, "mm/dd/yyyy   hh:mm:ss") & vbCrLf & vbCrLf
End Function

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now