I need to call a function and perform a cleanup only if the function fails, and then relaunch the error. Here is the pseudo-code:
(condition-case err
(call-function)
(error
(cleanup)
;; how do I raise `err`?
))
I need to call a function and perform a cleanup only if the function fails, and then relaunch the error. Here is the pseudo-code:
(condition-case err
(call-function)
(error
(cleanup)
;; how do I raise `err`?
))
You're looking for signal
:
(condition-case err
(call-function)
(error
(cleanup)
(signal (car err) (cdr err)))) ; reraise `err'
BTW, an alternative option can be something like:
(let ((error t))
(unwind-protect
(prog1 (call-function)
(setq error nil))
(when error (cleanup))))
The advantage is that you avoid catching&rethrowing the error, which means for example that the debugger will show you the right backtrace (the one that corresponds to the actual source of the error, rather than the one that corresponds to you re-throwing someone else's error).
Another differences is that cleanup
will be called not only upon error but also if the code is interrupted by a C-g
or a call to throw
.