⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 error.pm.svn-base

📁 解码器是基于短语的统计机器翻译系统的核心模块
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
	    }	    elsif(defined $wantarray) {		$result[0] = $try->();	    }	    else {		$try->();	    }	    1;	};	$err = defined($Error::THROWN) ? $Error::THROWN : $@	    unless $ok;    };    shift @Error::STACK;    $err = run_clauses($clauses,$err,wantarray,@result)	unless($ok);    $clauses->{'finally'}->()	if(defined($clauses->{'finally'}));    throw $err if defined($err);    wantarray ? @result : $result[0];}# Each clause adds a sub to the list of clauses. The finally clause is# always the last, and the otherwise clause is always added just before# the finally clause.## All clauses, except the finally clause, add a sub which takes one argument# this argument will be the error being thrown. The sub will return a code ref# if that clause can handle that error, otherwise undef is returned.## The otherwise clause adds a sub which unconditionally returns the users# code reference, this is why it is forced to be last.## The catch clause is defined in Error.pm, as the syntax causes it to# be called as a methodsub with (&;$) {    @_}sub finally (&) {    my $code = shift;    my $clauses = { 'finally' => $code };    $clauses;}# The except clause is a block which returns a hashref or a list of# key-value pairs, where the keys are the classes and the values are subs.sub except (&;$) {    my $code = shift;    my $clauses = shift || {};    my $catch = $clauses->{'catch'} ||= [];        my $sub = sub {	my $ref;	my(@array) = $code->($_[0]);	if(@array == 1 && ref($array[0])) {	    $ref = $array[0];	    $ref = [ %$ref ]		if(UNIVERSAL::isa($ref,'HASH'));	}	else {	    $ref = \@array;	}	@$ref    };    unshift @{$catch}, undef, $sub;    $clauses;}sub otherwise (&;$) {    my $code = shift;    my $clauses = shift || {};    if(exists $clauses->{'otherwise'}) {	require Carp;	Carp::croak("Multiple otherwise clauses");    }    $clauses->{'otherwise'} = $code;    $clauses;}1;__END__=head1 NAMEError - Error/exception handling in an OO-ish way=head1 SYNOPSIS    use Error qw(:try);    throw Error::Simple( "A simple error");    sub xyz {        ...	record Error::Simple("A simple error")	    and return;    }     unlink($file) or throw Error::Simple("$file: $!",$!);    try {	do_some_stuff();	die "error!" if $condition;	throw Error::Simple -text => "Oops!" if $other_condition;    }    catch Error::IO with {	my $E = shift;	print STDERR "File ", $E->{'-file'}, " had a problem\n";    }    except {	my $E = shift;	my $general_handler=sub {send_message $E->{-description}};	return {	    UserException1 => $general_handler,	    UserException2 => $general_handler	};    }    otherwise {	print STDERR "Well I don't know what to say\n";    }    finally {	close_the_garage_door_already(); # Should be reliable    }; # Don't forget the trailing ; or you might be surprised=head1 DESCRIPTIONThe C<Error> package provides two interfaces. Firstly C<Error> providesa procedural interface to exception handling. Secondly C<Error> is abase class for errors/exceptions that can either be thrown, forsubsequent catch, or can simply be recorded.Errors in the class C<Error> should not be thrown directly, but theuser should throw errors from a sub-class of C<Error>.=head1 PROCEDURAL INTERFACEC<Error> exports subroutines to perform exception handling. These willbe exported if the C<:try> tag is used in the C<use> line.=over 4=item try BLOCK CLAUSESC<try> is the main subroutine called by the user. All other subroutinesexported are clauses to the try subroutine.The BLOCK will be evaluated and, if no error is throw, try will returnthe result of the block.C<CLAUSES> are the subroutines below, which describe what to do in theevent of an error being thrown within BLOCK.=item catch CLASS with BLOCKThis clauses will cause all errors that satisfy C<$err-E<gt>isa(CLASS)>to be caught and handled by evaluating C<BLOCK>.C<BLOCK> will be passed two arguments. The first will be the errorbeing thrown. The second is a reference to a scalar variable. If thisvariable is set by the catch block then, on return from the catchblock, try will continue processing as if the catch block was neverfound.To propagate the error the catch block may call C<$err-E<gt>throw>If the scalar reference by the second argument is not set, and theerror is not thrown. Then the current try block will return with theresult from the catch block.=item except BLOCKWhen C<try> is looking for a handler, if an except clause is foundC<BLOCK> is evaluated. The return value from this block should be aHASHREF or a list of key-value pairs, where the keys are class namesand the values are CODE references for the handler of errors of thattype.=item otherwise BLOCKCatch any error by executing the code in C<BLOCK>When evaluated C<BLOCK> will be passed one argument, which will be theerror being processed.Only one otherwise block may be specified per try block=item finally BLOCKExecute the code in C<BLOCK> either after the code in the try block hassuccessfully completed, or if the try block throws an error thenC<BLOCK> will be executed after the handler has completed.If the handler throws an error then the error will be caught, thefinally block will be executed and the error will be re-thrown.Only one finally block may be specified per try block=back=head1 CLASS INTERFACE=head2 CONSTRUCTORSThe C<Error> object is implemented as a HASH. This HASH is initializedwith the arguments that are passed to it's constructor. The elementsthat are used by, or are retrievable by the C<Error> class are listedbelow, other classes may add to these.	-file	-line	-text	-value	-objectIf C<-file> or C<-line> are not specified in the constructor argumentsthen these will be initialized with the file name and line number wherethe constructor was called from.If the error is associated with an object then the object should bepassed as the C<-object> argument. This will allow the C<Error> packageto associate the error with the object.The C<Error> package remembers the last error created, and also thelast error associated with a package. This could either be the lasterror created by a sub in that package, or the last error which passedan object blessed into that package as the C<-object> argument.=over 4=item throw ( [ ARGS ] )Create a new C<Error> object and throw an error, which will be caughtby a surrounding C<try> block, if there is one. Otherwise it will causethe program to exit.C<throw> may also be called on an existing error to re-throw it.=item with ( [ ARGS ] )Create a new C<Error> object and returns it. This is defined forsyntactic sugar, eg    die with Some::Error ( ... );=item record ( [ ARGS ] )Create a new C<Error> object and returns it. This is defined forsyntactic sugar, eg    record Some::Error ( ... )	and return;=back=head2 STATIC METHODS=over 4=item prior ( [ PACKAGE ] )Return the last error created, or the last error associated withC<PACKAGE>=back=head2 OBJECT METHODS=over 4=item stacktraceIf the variable C<$Error::Debug> was non-zero when the error wascreated, then C<stacktrace> returns a string created by callingC<Carp::longmess>. If the variable was zero the C<stacktrace> returnsthe text of the error appended with the filename and line number ofwhere the error was created, providing the text does not end with anewline.=item objectThe object this error was associated with=item fileThe file where the constructor of this error was called from=item lineThe line where the constructor of this error was called from=item textThe text of the error=back=head2 OVERLOAD METHODS=over 4=item stringifyA method that converts the object into a string. This method may simplyreturn the same as the C<text> method, or it may append moreinformation. For example the file name and line number.By default this method returns the C<-text> argument that was passed tothe constructor, or the string C<"Died"> if none was given.=item valueA method that will return a value that can be associated with theerror. For example if an error was created due to the failure of asystem call, then this may return the numeric value of C<$!> at thetime.By default this method returns the C<-value> argument that was passedto the constructor.=back=head1 PRE-DEFINED ERROR CLASSES=over 4=item Error::SimpleThis class can be used to hold simple error strings and values. It'sconstructor takes two arguments. The first is a text value, the secondis a numeric value. These values are what will be returned by theoverload methods.If the text value ends with C<at file line 1> as $@ strings do, thenthis infomation will be used to set the C<-file> and C<-line> argumentsof the error object.This class is used internally if an eval'd block die's with an errorthat is a plain string.=back=head1 KNOWN BUGSNone, but that does not mean there are not any.=head1 AUTHORSGraham Barr <gbarr@pobox.com>The code that inspired me to write this was originally written byPeter Seibel <peter@weblogic.com> and adapted by Jesse Glick<jglick@sig.bsh.com>.=head1 MAINTAINERArun Kumar U <u_arunkumar@yahoo.com>=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -