runtime errors and exceptions
Errors can occur while programs run. Serious errors like memory faults are unexpected exceptions, while other errors, like overflow caused by improper user input are foreseeable and even preventable. Errors like reaching the end of a file during a read operation may even be counted on by programs.
When an error occur, the program continue running. Therefore programs can, and usually should test to see if an error occured after any operation or function call that could produce an error. Programs can then take appropriate actions depending on the source and nature of each error. Depending on the situation, programs can:
° ignore the error.
° retry the attempt.
° try to perform the operation in another way.
° request user assistance to work around the problem.
° terminate the program.
ERROR() and ERROR$()
When an error occurs, an error number is assigned to an internal ##ERROR system variable, which can be quickly and efficiently examined and/or modified with the error = ERROR(newError) intrinsic. error is the existing value of ##ERROR returned by ERROR(), and newError is a new error number assigned to ##ERROR. Set newError to 0 to clear the error to 0 to ready ##ERROR for the next error, to -1 to leave ##ERROR unaltered, or to an appropriate error number to report an error.
error$ = ERROR$(error) converts error into an error string.
runtime error handling
Many programs follow operations that can produce an error with:
IF ERROR(-1) THEN RETURN ' -1 means don't change value in ##ERROR
IF ERROR(-1) THEN ' detect error (no change to ##ERROR)
error = ERROR(0) ' clear ##ERROR
error = ERROR ($$ErrorNatureOverflow) ' get old error & set new one