Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Exception handling and "trap" vs. TRAPNAL()
- X-seq: zsh-workers 21834
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: DervishD <zsh@xxxxxxxxxxxx>
- Subject: Re: Exception handling and "trap" vs. TRAPNAL()
- Date: Tue, 04 Oct 2005 16:31:44 +0000
- Cc: zsh-workers@xxxxxxxxxx
- In-reply-to: <20051003175913.GB3231@DervishD>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- References: <20051001153756.GA12183@DervishD> <1051001183818.ZM27904@xxxxxxxxxxxxxxxxxxxxxxx> <20051001202856.GA134@DervishD> <1051002044052.ZM28373@xxxxxxxxxxxxxxxxxxxxxxx> <20051002190940.437F9866F@xxxxxxxxxxxxxxxxxxxxxxxx> <1051002195518.ZM2163@xxxxxxxxxxxxxxxxxxxxxxx> <20051002230027.GA194@DervishD> <1051003013758.ZM3107@xxxxxxxxxxxxxxxxxxxxxxx> <20051003090121.GC278@DervishD> <1051003162109.ZM4533@xxxxxxxxxxxxxxxxxxxxxxx> <20051003175913.GB3231@DervishD>
On Oct 3, 7:59pm, DervishD wrote:
:
: I want to replace this code:
:
: command1 && {# handle here some possible error probably exiting}
: command2 && {# handle here exactly the same error as before}
: ...
: commandn && {# incredible, here we must handle a similar error}
:
: with this one:
:
: trap 'throw commonerror$LINENO' ZERR
: {
: command1
: command2
: command3
: } always {
: # Here we catch and handle the common error
: # In the exception name we have the line number,
: # just in case we want to fine tune error handling
: }
First of all, note that unless command3 is return, the always block
is going to execute regardless of whether there has been an error.
How about:
function common_error() {
# Here we catch and handle the common error
# In the ERROR_LINE variable we have the line number,
# and in the ERROR variable we have the $? status,
# just in case we want to fine tune error handling
}
trap 'ERROR=$?; ERROR_LINE=$LINENO; return $ERROR' ZERR
trap common_error EXIT
command1
command2
command3
trap - EXIT ZERR
Note also for reference that
trap 'return $?' ZERR
is roughly equivalent to
setopt ERR_RETURN
Messages sorted by:
Reverse Date,
Date,
Thread,
Author