| Article ID: | T37 |
| Date: | 6/8/99 |
| Product: | SPEL for Windows (all versions) |
| OS: | Windows 3.11, Windows '95, Windows '98 and Windows NT 4.0 |
| Problem: | Error handler not retained after calling a function with error handler |
Symptoms
After calling a function that has it's own error
handler, subsequent errors will trigger the error handler in the called
function. The original error handler is not restored after the call.
In
the following example, function main calls test, which sets an error handler at
testErr. When main executes Jump P99, an error occurs (the point is not
defined) and execution is transferred to the error handler in test, not the
error handler in main.
Function main |
|
| OnErr mainErr Call test Print "Executing Jump from main" Jump P99 ' point is not defined End |
|
| mainErr: | |
| Print "Error", Err(0),
" in main error handler" EClr Return |
|
| Fend |
|
| Function test | |
| OnErr testErr Print "Executing test" End |
|
| testErr: | |
| Print "Error", Err(0),
" in test error handler" EClr Return |
|
| Fend | |
| Output: | |
| Executing test Executing Jump from main Error 78 in test error handler |
|
Cause
If you set error handlers for multiple functions
that are in the same task, any errors that occur will trigger the most recent
error handler that was executed with the OnErr statement.
Remedies
Method1:
Use one error handler for the entire task Use
OnErr in the main function to set the error handler. All functions that are
called will trigger the main error handler when errors occur, and the Return
statement will cause execution to continue in the function where the error
occurred.
| Method2: | |
| Execute OnErr
after calling the function with it's own error handler, as shown in this
example. |
|
Function main |
|
| OnErr mainErr ... Call test OnErr mainErr ' reset error handler after call Jump P99 End |
|
| mainErr: | |
| Print "Error", Err(0),
" in main" EClr Return |
|
| Fend |
|
| Function test | |
| OnErr testErr Print "test" End |
|
| testErr: | |
| Print "Error", Err(0), " in test"
EClr Return |
|
| Fend |
|
