From cc6612cb3268d8fe15e9359289bec426a9bee831 Mon Sep 17 00:00:00 2001 From: "ian%hixie.ch" Date: Wed, 25 Dec 2002 16:48:54 +0000 Subject: [PATCH] Handle exceptions raised inside exception blocks by reraising them after the finally block. This code is going to need some refactoring at some point... --- webtools/PLIF/PLIF/Exception.pm | 41 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/webtools/PLIF/PLIF/Exception.pm b/webtools/PLIF/PLIF/Exception.pm index f36b09e57049..f29b558b0742 100644 --- a/webtools/PLIF/PLIF/Exception.pm +++ b/webtools/PLIF/PLIF/Exception.pm @@ -121,13 +121,6 @@ sub try(&;$) { } }; if (defined($continuation)) { - if ($@ ne '') { - if (not ref($@) or - not $@->isa('PLIF::Exception')) { - # an unexpected exception - $@ = PLIF::Exception->create('message' => $@); - } - } $continuation->handle($@); return; } else { @@ -218,13 +211,31 @@ sub create { sub handle { my $self = shift; my($exception) = @_; + $self->{'resolved'} = 1; + if ($exception ne '') { + if (not ref($exception) or + not $exception->isa('PLIF::Exception')) { + # an unexpected exception + $exception = PLIF::Exception->create('message' => $exception); + } + } else { + $exception = undef; + } my $reraise = undef; handler: while (1) { if (defined($exception)) { foreach my $handler (@{$self->{'handlers'}}) { if ($exception->isa($handler->[0])) { - my $result = &{$handler->[1]}($exception); - if (not defined($result) or + my $result = eval { &{$handler->[1]}($exception) }; + if ($@) { + if (not ref($@) or + not $@->isa('PLIF::Exception')) { + # an unexpected exception + $@ = PLIF::Exception->create('message' => $@); + } + $reraise = $@; + last handler; + } elsif (not defined($result) or not ref($result) or not $result->isa('PLIF::Exception::Internal::Unhandled')) { last handler; @@ -234,7 +245,15 @@ sub handle { } } if (defined($self->{'except'})) { - &{$self->{'except'}}($exception); + my $result = eval { &{$self->{'except'}}($exception) }; + if ($@) { + if (not ref($@) or + not $@->isa('PLIF::Exception')) { + # an unexpected exception + $@ = PLIF::Exception->create('message' => $@); + } + $reraise = $@; + } } else { # unhandled exception $reraise = $exception; @@ -245,8 +264,8 @@ sub handle { if (defined($self->{'finally'})) { &{$self->{'finally'}}(); } - $self->{'resolved'} = 1; if (defined($reraise)) { + # note that if the finally block raises an exception, we drop this one $reraise->raise(); } }