Wiki Home

On Error


Namespace: VB
From the VFP Help file:
ON ERROR [Command] specifies the Microsoft Visual FoxPro command to execute. After the command executes, program execution resumes on the line immediately following the line that caused the error. However, if the error-handling procedure includes RETRY, the program line that caused the error is executed again.

If the command specifies a procedure to execute when an error occurs, you can use ERROR(), MESSAGE(), LINENO(), and PROGRAM() to pass the error number, the error message, the program line number, and the program name to the procedure. This information can be used to correct the cause of the error.

Remarks: When an error occurs during program execution, Visual FoxPro executes the command you specify with ON ERROR. Typically, ON ERROR uses DO to execute an error-handling procedure.

Use ON ERROR without a command to restore the default Visual FoxPro error handler.

ON ERROR procedures cannot be nested. If ON ERROR is issued within an ON ERROR procedure, the default Visual FoxPro error handler is restored.


The last sentence in the above quote (about nested "on error" statements) is untrue, at least in VFP 7. For example, in VFP 7, the following code works:

local sPreviousErrorHandler
m.sPreviousErrorHandler=on("error")   && Save the previous error handler.
on error HandleError(m.sPreviousErrorHandler)   && If there's an error, call our local error handler.
use nonexistent_table   && Trip our local error handler.

function HandleError(sPreviousHandler)
   messagebox("Ouch!  Returning to previous error handler.")
   on error &sPreviousHandler
endfunc


Notice that the "on error" command inside of the "HandleError" function is nested. Even the following, more obviously nested command works:
on error on error return   && Don't return on the first error, but do on the second one.


When in Development Mode, it's always useful to handle errors differently than in Production Mode.

In Production Mode, typically your ON ERROR should trigger modal dialog with the error, a suggestion for resolution or to contact the developer, and an OK button (among others).

In Development Mode, your ON ERROR should bomb you back into your development environment, or trigger a different custom dialog which includes, at minimum, ok, cancel, and debug buttons. This allows easy access to the environment for debugging errors.
There is a nifty lightweight and functional Error Handler in the Fox Foundation Classes
From the Hackers Guide:
ON ERROR is the last resort for errors that have stumped the local Error method, or occur in a place where those procedures do not have control. There are a huge number of errors, but most fall into a small number of categories: catastrophic, and worse. Our general feeling is that the error handler should record as much information about the error as possible (storing such values as LINENO(), PROGRAM() and lots of SYS() values to a text file or memo field) and then shut down the operation.

On Error does not require a separate program; you can just enter a single line of code.

On Error ? "#" + trans( error()) + " " + message()
On Error lnError = error()
On Error lnErrCnt = lnErrCnt+1
On Error return


This one is good for bug reports (funny that it includes a workaround to the && bug) :
On Error _cliptext = "&" + "& error #" + transform( error() ) + ;
   ' "' + message() + '" Line #' + transform( lineno() ) + ' "' + message( 1 ) + '"'


Here is a simple general on error: (untested)
On Error messagebox( 'error #' + transform( error() ) + ;
   ' "' + message() + '"' + chr(13) + 'Line #' + ;
   transform( lineno() ) + chr(13) +  message(1) )

My error handler was freaking out, so I added this - it helped.
LPARAMETERS 	tnError, 	tcMethod, 	tnLineNo
messagebox( 'error #' + transform( error() ) + ': "' + message() + '"' + chr(13) ;
	+ 'program:  ' + Program( Program(-1)-1) + chr(13) ;
	+ 'tcMethod:  ' + tcMethod + chr(13) ;
	+ 'line #' + transform( lineno() ) + ":  " + message(1) )

DoDefault( tnError, 	tcMethod, 	tnLineNo )

Return


To turn off the error handler issue:

ON ERROR *


This is useful, for example, when using LOCFILE(). If the user selects "Cancel" in a LOCFILE dialog, the VFP error handler is invoked.

lcOldError = ON("ERROR")
ON ERROR *
lcImage = LOCFILE("", "Images:GIF,BMP", "&Locate image:")
ON ERROR &lcOldError.

See Also Error Information, Try Catch, Structured Error Handling
Category Error Handling Category Exam 70-155 Hot Topic Category 3 Star Topics
Contributors: Michael GEmmons, Steven Black, Carl Karsten, David TAnderson, Kurtis Oliverson
( Topic last updated: 2009.08.06 02:42:33 PM )