Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 - How to catch exception or error during runtime

I developed an application in VB6. In client's environment it raises runtime errors which I can't reproduce under debugger. Is there any way to get the stacktrace or location of error? I created log file and

I used Err.Description,Err.Source but it gives blank values.

Please help me.

 my method(......

    On Error GoTo Error_Handler

             .........
    Error_Handler : 
                  writeToLogFile(Err.Source,Err.Description)
like image 254
Royson Avatar asked Jan 19 '10 07:01

Royson


3 Answers

You've probably done something to clear the Err object before writing to the log file. This is very, very easy to do. What you'll want to do is as soon as you detect an error has occurred, grab the error message before doing anything else. Then pass the error message to whatever logging routine you're using. E.g.:

Dim sMsg As String

On Error Goto ErrHandler

' ...code here...

Exit Function

ErrHandler:
sMsg = "Error #" & Err.Number & ": '" & Err.Description & "' from '" & Err.Source & "'"
GoLogTheError sMsg
like image 179
T.J. Crowder Avatar answered Nov 09 '22 11:11

T.J. Crowder


BTW, thanks for your guys' answers helping me. I'm about half a decade late to the game of VB6. I don't do windows unless forced to. ;)

Anyhow, when doing your error checking, say among 3000 individual record query insertions, I learned a couple tricks. Consider this block of code:

'----- order number 1246-------
On Error Goto EH1246:
sSql="insert into SalesReceiptLine ( CustomerRefListID,TemplateRe..."
oConnection.Execute sSQL
sSql="SELECT TxnID FROM SalesReceiptLine WHERE RefNumber='1246'..."
  oRecordset.Open sSQL, oConnection
sTxnId = oRecordset(0)
  oRecordset.Close
sSql="INSERT INTO SalesReceiptLine (TxnId,SalesReceiptLineDesc,Sal..."
  oConnection.Execute sSQL
EH1246:
IF Err.Number<>0 THEN
    sMsg = sMsg & "Order # 1246; sTxnId = " & sTxnId & _
        vbCrLf & Err.Number & ": " & Err.Description & vbCrLf
sErrOrders = sErrOrders & "1246,"
End If
On Error GoTo -1
'----- order number 1247-------
On Error Goto EH1247:

When not testing for Err.Number, you'll get a 0: on every order handled. (maybe you don't want that). The On Error GoTo -1 resets the error so that it will work again. Apparently, Err only works "once".

I wrote a php script to build the VB6 source code to run some 8000 odbc queries... :P

like image 2
Krista K Avatar answered Nov 09 '22 11:11

Krista K


Do you definitely, positively have an Exit Function just above the Error_Handler:?

like image 1
MarkJ Avatar answered Nov 09 '22 11:11

MarkJ