unwindProtect
unwindProtect( [g_protectedForm] [g_cleanupForm] ) =>g_result
Description
Evaluates the g_protectedForm expression and then executes the SKILL g_cleanupForm expression. Even if the evaluation of g_protectedForm is interrupted or encounters an error, the g_cleanupForm expression is still executed. You can therefore use g_cleanupForm to close open file handles, reset variables, and restore the state to a known value.
If an error occurs within g_protectedForm, the program would normally stop after executing g_cleanupForm. To force continued execution despite the error, wrap unwindProtect with a suitable function to catch errors, such as errset. Even if the error is caught using errset, g_cleanupForm will still be executed.
To include more than a single expression, group expressions by using functions such as progn, let, or prog.
Arguments
Value Returned
Examples
unwindProtect(undefFun() printf("cleanup form called here\n"))
*Error* eval: undefined function - undefFun
cleanup form called here
unwindProtect( { printf("first statement\n")
1/0
printf("second statement\n")
}
printf("cleanup form called here\n")
)
first statement
*Error* quotient: Attempted to divide by zero
cleanup form called here
errset( unwindProtect( {
printf("first statement\n")
1/0
printf("second statement\n")
}
printf("cleanup form called here\n")
)
)
printf("program continued after error\n")
first statement
cleanup form called here
program continued after error
Related Topics
Function and Program Structure
Return to top