Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan ....EndScan 1

Status
Not open for further replies.

sohrab100

Programmer
Jan 18, 2002
56
HK
E.g 1
Scan
....
****
++++
++++
EndScan

e.g 2
For
....
****
++++
++++
Endfor
if **** cause an error, i want to catch the error no by on error with a error handling function and skip the rest of the command ++++ and skip current record and continue the next record, how can i do it?

sohrab100
 
sohrab100,

Scan
nError = 0
On error nError = error()
**** Any command
If (nError == 0)
? 'No error'
else
? 'ERROR !!!'
endif
Endscan


-- AirCon --
 
sohrab100,

Sorry, a little modification :)

nError = 0
On error nError = error()
Scan
nError = 0

**** Your code goes here

If (nError == 0)
? 'No error'
else
? 'ERROR !!!'
endif
Endscan
On error

Regards

-- AirCon --
 
If i understand good what you want:
Scan
....
****
if error
loop
endif
++++
++++
EndScan



Monika from Warszawa (Poland)
(monikai@yahoo.com)
 
Sohrab100 ,

This is an ideal use for the new TRY / CATCH /FINNALY construct in VFP 8.0.

However, if you are using VFP 7.0 or below, I would suggest you go with AirCon's solution, but with some added code to save and restore your existing error handler (if any).

To save the existing error handler:

lcOldErrHandler = ON("ERROR")

To restore it:

ON ERROR &lcOldErrHandler

Hope that helps.

Mike


Mike Lewis
Edinburgh, Scotland
 
Mike

Ahh..I forgot that things :-D


-- AirCon --
 
All of the other posts are correct, but if I'm not
mistaken, I believe you wanted to bypass a block of
code if there is an error.

Without Try-Catch, there are only two ways I can
think of to accomplish this:

1- Encapsulate each code line with an error test
(not quite the spirit of your request)

-or-

2- Call out to a block of code in a
function/procedure/method from within
the scan...endscan (My preference)

Here's some kludge code showing the concept:

CLEAR
LOCAL lcCurErrHand
lcCurErrHand = ON("error")

CREATE CURSOR test (red N(3), green N(3), blue N(3), cText c(60))

FOR i = 0 TO 255
INSERT INTO test (red, green, blue, cText) VALUES ;
(i, ;
IIF(MOD(i,20)==0,500,i), ;
IIF(MOD(i,20)==0,300,i), ;
IIF(MOD(i,20)==0,"This record will cause an error","Good record "+ALLT(STR(i))))
NEXT

gnError = 0 && Just as an example
ON ERROR Err_Hand(ERROR(), MESSAGE(), MESSAGE(1), PROG(), LINENO(), @gnError)

GO TOP
SCAN
DO TheCode && Call the inner scan code
IF gnError <> 0 && Did we get an error?
?
? &quot;Got an error&quot;+STR(gnError),&quot; at record &quot;,RECNO()-1
?
gnError = 0
ENDIF
ENDSCAN

ON error &lcCurErrHand

PROCEDURE TheCode
LOCAL lcRGB
* Error trap will catch errors caused by any
* command that follows
lcRGB = RGB(red,green,blue)
? &quot;RGB value&quot;,lcRGB,&quot; &quot;
?? cText
ENDPROC

FUNCTION Err_Hand(nErr, cMsg1, cMsg2, cProg, nLin, lnGlobalErr)
lnGlobalErr = nErr
DO CASE
CASE lnGlobalErr == 11 && Function argument value, type, or count is invalid.
* Find the process that's above the procedure that
* caused the error in the call stack
LOCAL i, lcReturnTo
i = 0
DO WHILE SYS(16,i) <> &quot;ON..&quot;
i = i + 1
ENDDO
lcReturnTo = SYS(16,i-2)
RETURN TO &lcReturnTo
OTHERWISE
MESSAGEBOX(&quot;Un-Anticipated Error&quot;,16,&quot;Bomb!&quot;)
susp
CANCEL
ENDCASE
ENDFUNC



Darrell


'We all must do the hard bits so when we get bit we know where to bite' :)
 
oops! Didn't read it properly. All the methods posted work!

Darrell


'We all must do the hard bits so when we get bit we know where to bite' :)
 
PUBLIC blnContinue
Local cErrorHandling, nCounter, nMax
cErrorHandling = ON(&quot;ERROR&quot;)

On Error Do showerror With ;
ERROR( ), MESSAGE( ), MESSAGE(1), PROGRAM( ), LINENO(1)

*****************
*E.g 1
*****************
SCAN
blnContinue = .T.
***Line of Code ....
***Line of Code .... &&If this throws an error then it will loop up to Scan again
IF !blnContinue
LOOP
endif
***Line of Code .... &&Will only run if no error
***Line of Code .... &&Will only run if no error
ENDSCAN
*****************
* e.g 2
*****************
nMax = 20
FOR nCounter = 1 TO nMax
blnContinue = .T.
***Line of Code ....
***Line of Code .... &&If this throws an error then it will loop up to Scan again
IF !blnContinue
LOOP
endif
***Line of Code .... &&Will only run if no error
***Line of Code .... &&Will only run if no error
ENDFOR

On Error &cErrorHandling &&Restore original error handling
Release blnContinue

****************************************************************
Procedure showerror(nError, cMessage, cMessage1, cProgram, nLineno)
****************************************************************
Local cFeedback, cWinTempDir, cDateTime
cDateTime = TTOC(DATETIME())+Chr(13)
cFeedback='Error number: '+Ltrim(Str(nError))+Chr(13) ;
+'Error message: ' + cMessage + Chr(13) ;
+'Line of code with error: ' + cMessage1+ Chr(13);
+'Line number of error: '+Ltrim(Str(nLineno))+Chr(13);
+ 'Program with error: ' + cProgram
cWinTempDir = ADDBS(SYS(2023))
Strtofile(cDateTime + cFeedback,cWinTempDir+&quot;MyErrorFile.txt&quot;,.T.) &&Log Error Information in text file in temp directory
=Messagebox(cFeedback,&quot;ERROR !!!&quot;,16) &&Show Error Message in messagebox
if Type(&quot;blnContinue&quot;) != &quot;U&quot;
blnContinue = .F.
endif
ENDPROC

Slighthaze = NULL
 
Actually, it's a restore program.
e.g.
dbfname='company'
use path + dbfname in 0 alias db1 excl
Scan
codetable = path + db1.code
select othertable
append from (codetable)
***operation for copy data db1 to a new table
***other operation
endscan

The case that is when my client restore the data, it have error on 114,15 (Index does not match, Not a Table resp.)
i knowledge that the old data of my client have some table corrupted and i want to check out which table is corrupted or have problem, so when i execute the problem, error popup and i want to skip current record and the rest of line of code (of course log it down) and continue next record (means next table).
Therefore, what i want is to know how to catch up all the error events for every line inside the scan..endscan and skip the rest of line of code inside the scan..endscan after the error occur and continue next record.
Am i make the case clear?

sohrab100
 
sohrab100,

my suggestion creates a log as errors are created and continues to append the errors to the log file in windows temp directory as they happen...you could easily add the table name since you are already holding it in the codetable variable. If you don't want the other error information just take it out and you can do the same with the messagebox if you don't want that. You could even use some of the FAQ's in this forum to make the code email you the MyErrorFile.txt file if there were any errors. Modify my code to suit...


PUBLIC blnContinue
Local cErrorHandling, nCounter, nMax
cErrorHandling = ON(&quot;ERROR&quot;)
codetable = &quot;&quot;
On Error Do showerror With ;
ERROR( ), MESSAGE( ), MESSAGE(1), PROGRAM( ), LINENO(1), codetable

dbfname='company'
use path + dbfname in 0 alias db1 excl
Scan
blnContinue = .T.
codetable = path + db1.code
select othertable
append from (codetable)
IF !blnContinue
LOOP
endif
***operation for copy data db1 to a new table
***other operation
endscan

On Error &cErrorHandling &&Restore original error handling
Release blnContinue

****************************************************************
Procedure showerror(nError, cMessage, cMessage1, cProgram, nLineno, cTable)
****************************************************************
Local cFeedback, cWinTempDir, cDateTime
cDateTime = TTOC(DATETIME())+Chr(13)
cFeedback='Error number: '+Ltrim(Str(nError))+Chr(13) ;
+'Error message: ' + cMessage + Chr(13) ;
+'Line of code with error: ' + cMessage1+ Chr(13);
+'Line number of error: '+Ltrim(Str(nLineno))+Chr(13);
+ 'Program with error: ' + cProgram;
+ 'Table with error: ' + cTable
cWinTempDir = ADDBS(SYS(2023))
Strtofile(cDateTime + cFeedback,cWinTempDir+&quot;MyErrorFile.txt&quot;,.T.) &&Log Error Information in text file in temp directory
=Messagebox(cFeedback,&quot;ERROR !!!&quot;,16) &&Show Error Message in messagebox
if Type(&quot;blnContinue&quot;) != &quot;U&quot;
blnContinue = .F.
endif
ENDPROC

Slighthaze = NULL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top