Wiki Home

Com Error Handling


Namespace: B2B
Currently this is an enhancement to COMC omponent Example. Please expand.


Add these properties and procedures to the cPerson class:

	nError = 0
	cErrorMessage = ""

	procedure Error( tnError, tcMethod, tnLine )
		local lcErrMsg
		lcErrMsg = ttoc(datetime()) + ;
			": error #" + transform(tnError) + ;
			' "' + message() + ;
			'" -- Method: "' + tcMethod + ;
			'" Line #' + transform(tnLine) + ;
			' "' + message(1) + '"'
		this.nError = tnError
		this.cErrorMessage = lcErrMsg
		strToFile( lcErrMsg + chr(13), "error.log", .t. )
	endproc
	
	procedure ErrorTest
		error 'Expected error.'
		return -1
	endproc


To test in VFP:
oPerson = CREATEOBJECT("comdata.cPerson")
? oPerson.errortest()

You should see a -1, and there should be the file error.log left in your current dir. type error.log to see it's contents.

In Excell:
Public oPerson As Object
Sub Beatles()
    Set oPerson = CreateObject("comdata.cperson")
    If oPerson.nError <> 0 Then
        ActiveSheet.Cells(1, 1).Value = oPerson.nError
    Else
        ActiveSheet.Cells(1, 1).Value = oPerson.FirstName
        ActiveSheet.Cells(1, 2).Value = oPerson.LastName
        oPerson.goNext
        ActiveSheet.Cells(2, 1).Value = oPerson.FirstName
        ActiveSheet.Cells(2, 2).Value = oPerson.LastName
        oPerson.FindInName ("mccar")
        ActiveSheet.Cells(3, 1).Value = oPerson.FullName()
    End If
    Set oPerson = Nothing
End Sub

Contributors Carl Karsten


************************************************
You are going to get a -1 regardless of an error happening or not.
You would have to check the log to see if anything bad happened.
If you add

COMRETURNERROR('cPerson',lcErrMsg)


in the error routine above,
just before the ENDPROC (after the STRTOFILE...)

and create your test program as a prg with the followoing code
you will see the error message in your calling program where you
might be able to do something about it.

On error do errorhandler
oPerson = CREATEOBJECT("comdata.cPerson")
? oPerson.errortest()
* release the object from memory
Release oPerson

Proc errorhandler
   Local laError(1),i,ncErr
   Aerror(laError)
   ncErr = str(laError[1])
   For i = 2 to 5  && six returns numeric 0, it is usually char or NULL
	ncErr = ncErr+chr(13)+nvl(laError[i],'NULL')
   Next
   messagebox(ncErr)
endproc


Hale Pringle
Category C _ O _ M Category Exam 70-155 Category Error Handling
( Topic last updated: 2001.07.12 02:24:03 AM )