Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to trap errors with ON ERROR while error event in pl
Message
 
To
12/02/2002 15:05:59
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00618670
Message ID:
00619165
Views:
16
Hi Mark,

You've got the basic idea, but you have somewhat redefined the problem. Your approach will allow errors to be trapped, but it won't respect any ON ERROR logic that may be contained in the programs or UDFs that are executed in this fashion. If this is good enough for your immediate purposes, that's fine.

Note however that Doug Hennig's suggestion leads to the solution of a more general problem. It allows an "old-style" UDF that contains an ON ERROR handler to perform its own error handling as originally intended, even in an OO context. This is nice, because it increases the utility of simple, self-contained UDFs and can extend the life of legacy (non-OO) programs that would otherwise cease to handle errors properly. I think that is what Nadya would like to accomplish, for example.

Mike

>Thanks Everyone for your input. I like the wrapper idea, and created the following small function that should do what I need. Is this what you had in mind? Should it include any other functionality? Thanks. Mark
>
>FUNCTION ErrTrap
>* Trap for an error when executing a statement and return the error number if any, or 0.
>* Creates an object (oErrTrap) that is used to Trap Errors through its Error() event.
>* This is necessary to use (instead of ON ERROR) when a calling object has Error() event code.
>* This is because Error() event code gets called before any ON ERROR error trapping.
>* pass:
>* m.tcExecStmt = statement to execute as a macro command.
>* return:
>* Error number, or 0 if no error.
>*
>* To use:
>* m.lnErrNo= ErrTrap("statement I want to execute as a macro command")
>*
>* Or use this example to execute an expression and retrieve the result:
>* m.lcExpResult= "" && init expression result
>* m.lnErrNo= ErrTrap("m.lcExpResult= DbGetProp('table', 'view', 'comment')")
>* ** if the DbGetProp() doesn't get an error, the result will be in m.lcExpResult.
>
>LPARAMETERS m.tcExecStmt
>
>LOCAL oErrTrap, m.lnErrNo
>
>oErrTrap= CREATEOBJECT("oErrTrap")
>m.lnErrNo= oErrTrap.Execute(m.tcExecStmt) && execute stmt and get err no. if any
>RELEASE oErrTrap
>
>RETURN m.lnErrNo
>
>****************
>DEFINE CLASS oErrTrap AS separator
>
> nErrNo= 0 && property to hold error no.
>
> **********
> function Execute(m.tcExecStmt)
> &tcExecStmt && execute the statement
> RETURN this.nErrNo && return error no. if any
> ENDFUNC
>
> **********
> PROTECTED function Error(m.tnError, m.tcMethod, m.tnLine)
> this.nErrNo= m.tnError && set Error No. property
> ENDFUNC
>
>ENDDEFINE
>
>*****************
>
>>Hi Mark,
>>
>>You can't get ON ERROR to take precedence over an Error method of an object in the program stack. However, you could either move your code into method of a Custom class object, or wrap your UDF with an object, whose Error event method would then be usable for trapping errors as you intend. Even if you leave the code in a UDF and have a method of your wrapper object call it, the wrapper's Error event will catch any errors encountered in the PRG.
>>
>>Don't hold your breath on getting any major revisions to VFP's arcane error handling features. See http://www.ideaxchg.com/ix07/er/_sys/toccontu.htm for more about this subject, including a downloadable demo.
>>
>>Mike
>>
>>>Does anyone know of a way to set an ON ERROR statement in a prg and not have an actual Error passed back up to an Error() Event in an object that called the prg?
>>>
>>>For instance, in the CodeMine framework, Error trapping code is in the application object Error() event as well as in other classes. I want to call a utility prg which does its own error trapping. However, the ON ERROR's in the prg get ignored. I would really like a way to override the errors being passed up to the error event, and trap them right there.
>>>
>>>It seems that VFP needS an optional switch on the ON ERROR statement that will allow an ON ERROR statement to supercede an Error() event.
>>>
>>>
>>>For example, my prg might have code as follows:
>>>
>>>m.lcSavOnError= on("error")
>>>m.llFlag= .f.
>>>ON ERROR m.llFlag= .t.
>>>
>>>Use mytable
>>>
>>>ON ERROR &lcSavOnError
>>>
>>>if m.llFlag
>>> *..display error we got when using table
>>>endif
>>>
>>>Thanks in advance.
Montage

"Free at last..."
Previous
Reply
Map
View

Click here to load this message in the networking platform