NAME
Try::Tiny::Extended - Try::Tiny with some additional features
VERSION
Version 0.1
SYNOPSIS
use Try::Tiny::Extended;
# call some code and just silence errors:
try sub {
# some code which my die
};
# call some code with expanded error handling (throw exceptions as object)
try sub {
die (Exception1->new ('some error'));
},
catch 'Exception1' => sub {
# handle Exception1 exception
},
catch ['Exception2', 'Exception3'] => sub {
# handle Exception2 or Exception3 exception
},
catch_all sub {
# handle all other exceptions
},
finally sub {
# and finally run some other code
};
# call some code with expanded error handling (throw exceptions as strings)
try sub {
die ('some error1');
},
catch 'error1' => sub {
# search for 'error1' in message
},
catch qr/error\d/ => sub {
# search exceptions matching message to regexp
},
catch ['error2', qr/error\d/'] => sub {
# search for 'error2' or match 'error\d in message
},
catch_all sub {
# handle all other exceptions
},
finally sub {
# and finally run some other code
};
DESCRIPTION
Try::Tiny::Extended
is a simple way to handle exceptions. It's mostly a copy of Try::Tiny
module by Yuval Kogman, but with some additional features I need.
Main goal for this changes is to add ability to catch only desired exceptions. Additionally, it uses no more anonymous subroutines - there are public sub's definitions. This gave you less chances to forgot that return
statement exits just from exception handler, not surrounding function call.
If you want to read about other assumptions, read about our predecessor: Try::Tiny.
EXPORT
All functions are exported by default using Exporter.
SUBROUTINES/METHODS
try ($;@)
Works like Try::Tiny try
subroutine, here is nothing to add :)
The only difference is that here must be given evident sub reference, not anonymous block:
try sub {
# some code
};
catch ($$;@)
Intended to be used in the second argument position of try
.
Works similarly to Try::Tiny catch
subroutine, but have a little different syntax:
try sub {
# some code
},
catch 'Exception1' => sub {
# catch only Exception1 exception
},
catch ['Exception1', 'Exception2'] => sub {
# catch Exception2 or Exception3 exceptions
};
If raised exception is a blessed reference (or object), Exception1
means that exception class has to be or inherits from Exception1
class. In other case, it search for given string in exception message (using index
function or regular expressions - depending on type of given operator). For example:
try sub {
die ('some exception message');
},
catch 'exception' => sub {
say 'exception caught!';
};
Other case:
try sub {
die ('some exception3 message');
},
catch qr/exception\d/ => sub {
say 'exception caught!';
};
Or:
try sub {
# ValueError extends RuntimeError
die (ValueError->new ('Some error message'));
},
catch 'RuntimeError' => sub {
say 'RuntimeError exception caught!';
};
catch_all ($;@)
Works exactly like Try::Tiny catch
function (OK, there is difference: need to specify evident sub block instead of anonymous block):
try sub {
# some code
},
catch_all sub {
say 'caught every exception';
};
finally ($;@)
Works exactly like Try::Tiny finally
function (OK, again, evident sub instead of anonymous):
try sub {
# some code
},
finally sub {
say 'executed always';
};
SEE ALSO
- Try::Tiny
-
Minimal try/catch with proper localization of $@, base of Try::Catch::Extended
- TryCatch
-
First class try catch semantics for Perl, without source filters.
AUTHOR
Marcin Sztolcman, <marcin at urzenia.net>
BUGS
Please report any bugs or feature requests through the web interface at https://github.com/mysz/try-tiny-extended/issues.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Try::Tiny::Extended
You can also look for information at:
Try::Tiny::Extended home & source code
Issue tracker (report bugs here)
Search CPAN
ACKNOWLEDGEMENTS
Yuval Kogman for his Try::Tiny module :)
LICENSE AND COPYRIGHT
Copyright (c) 2012 Marcin Sztolcman. All rights reserved.
Base code is borrowed from Yuval Kogman L<Try::Tiny> module,
released under MIT License.
This program is free software; you can redistribute
it and/or modify it under the terms of the MIT license.