Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Design question - how to interrupt process
Message
From
01/06/2005 16:36:30
 
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01018971
Message ID:
01019133
Views:
23
This message has been marked as the solution to the initial question of the thread.
>>I readed the VFP Help file on TRY CATCH, and it states that the ERROR event will catch the objects error before the CATCH statement. In otherwords, if there is a Error event, it will process the error first. I also saw no means to raise the error to the CATCH statement from the Error event. But, if there is no Error event defined in the object, the CATCH statement does process the error.
>>
>>
>>From the Error event help
>>Any errors that occur in an object's Error event must be handled by the object and are not escalated to an ON ERROR routine or TRY...CATCH...FINALLY handler.
>>

>>
>>
>I guess instead of having that code in Error method, I can put similar code in the calling procedure, but I really would like to hide the logic...

Ok

Walk this through your debugger...
SET STEP ON

*-- Create the Object.
LOCAL ox
ox = CREATEOBJECT("DoThis")

TRY
	*-- Run your process
	ox.RunThis()
FINALLY
	*-- Release the Object.
	RELEASE ox
ENDTRY

*================

DEFINE CLASS doThis AS CUSTOM

	PROCEDURE MyError
		LPARAMETERS oErr AS EXCEPTION

		*-- Write message to file.
		LOCAL cMsg
		cMsg = "My method failed - Line "+TRANSFORM(oErr.LINENO )+" of "+TRANSFORM(oErr.PROCEDURE)
		STRTOFILE(cMsg, "Error.log", .T.)

		RETURN 

	ENDPROC

	PROCEDURE RunThis
		*-- do somthing and fail.
		LOCAL i AS INTEGER 				&& declare outside the TRY block so it is visible outside the TRY block.
		*-- start of method

		LOCAL oErr AS EXCEPTION
		TRY
			FOR i=1 TO 10
				?? "."
			NEXT

			*-- something happens, and the method fails,
			*-- so call ERROR with your cError message
			a=b		&& this will throw an error

			*-- Because the ERROR raised the error, and the CATCH caught it.
			*-- This part of the method is not ran.
			FOR i=1 TO 10
				?? "."
			NEXT

		CATCH TO oErr

			*-- catch the errors
			THIS.MyError( oErr )

		FINALLY
			*-- do what ever clean up that is necessary to leave method properly.
			RELEASE i, oErr
			
		ENDTRY

	ENDPROC

ENDDEFINE
The true TRY CATCH is inside the calling method, along with the error trap method. If the error trap method is defined in the parent or base class, the better.
Greg Reichert
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform