PV-WAVE Foundation > Tutorial > Creating PV-WAVE Applications > Error Handling in Procedures
Error Handling in Procedures
When an error occurs during the execution of a user-written procedure, a description of the error is printed and execution of the procedure halts. You can change the environment that is restored after an error occurs with the ON_ERROR procedure. The four possible actions are:
*0—Stop at the statement in procedure that caused the error, the default.
*1—Return all the way back to the main program level.
*2—Return to the caller of the program unit which established the ON_ERROR condition.
*3—Return to program unit which established ON_ERROR condition.
If ON_ERROR is not called by a parent of the procedure in which an error occurs, the procedure is not exited, and the current variables are those of the halted procedure, not of the caller. To return to the calling unit, or to the single statement mode if the procedure was called from the single statement mode, enter RETURN or RETALL at the WAVE> prompt.
Calling ON_ERROR from the main level or from a procedure sets the default error action for all modules called from that level. For example, to always return to the main level after an error, issue the statement:
ON_ERROR, 1
from the main level, or from your startup procedure.
Many library procedures issue an ON_ERROR, 2 call to return to their caller if an error occurs.
Error Signaling
Use MESSAGE in user-written procedures and functions to issue errors.
“Disappearing Variables”
PV‑WAVE novices are frequently dismayed to find that all their variables have seemingly disappeared after an error occurs inside a procedure or function. The misunderstood subtlety is that after the error occurs PV‑WAVE’s context is inside the called procedure, not in the main level. The variables still exist, but are defined within the scope of a different program unit. RETURN or RETALL allow you to walk back through the program unit stack to change the current scope. Note that you can only go back to previous scopes, you cannot return to the scope you exited.
RETALL is used when an error is detected in a procedure and you want to return immediately to the main program level from nested procedure calls. RETALL issues RETURNs until the main program level is reached.