NAME
Try::Catch - Try Catch exception handler based on Try::Tiny But faster
SYNOPSIS
use Try::Catch;
try {
die "something went wrong";
} catch {
} finally {
##some cleanup code
}; ##<--- semi colon is required.
DESCRIPTION
A small, fast, try catch blocks for perl, it's inspired and mostly copied from Try::Tiny but with some modifications to boost execution speed, see "Benchmarks".
I published a new module instead of contributing to Try::Tiny directly because I had to break some features available in Try::Tiny some to boost speed and some because I didn't like.
Differences
- no multiple finally blocks
- try must be followed by catch, catch then finally, or finally
-
this behaves exactly as how other implementations of try catch blocks
- if there is no catch block error will throw
-
in case of try followed by finally block and no catch block, finally block will be fired then an exception will be thrown, this is also the default behaviour of try catch in other languages.
CAVEATS
Same as "CAVEATS" in Try::Tiny
Benchmarks
This is not totally fair but please consider Try::Catch a stripped Try::Tiny version with no blessing and no usage of Sub::Name, so it must be faster, right! :)
This is a simple test with just a try catch blocks with no exception
| Module: | Rate | % |
|-------------------------------------------|
| Try::Tiny | 98425/s | -68% |
| Try::Catch | 304878/s | 210% |
Test with Try Catch, Finally Blocks, No Exception
| Module: | Rate | % |
|-------------------------------------------|
| Try::Tiny | 60423/s | -75% |
| Try::Catch | 245700/s | 304% |
Test with Try, Catch, Finally Blocks, AND Exception
| Module: | Rate | % |
|-------------------------------------------|
| Try::Tiny | 41288/s | -65% |
| Try::Catch | 116414/s | 182% |
I've also tested against TryCatch and the results were good, considering that TryCatch is an XS module
| Module: | timing 500000 iterations |
|----------------------------------------------------------------------- |
| TryCatch | 1 secs (0.58 usr + 0.00 sys = 0.58 CPU) @ 865051.90/s |
| Try::Catch | 2 secs (1.73 usr + 0.00 sys = 1.73 CPU) @ 288350.63/s |
| Try::Tiny | 6 secs (6.16 usr + 0.02 sys = 6.17 CPU) @ 81011.02/s |
Benchmarks included in this dist inside bench folder
See Also
AUTHOR
Mamod A. Mehyar, <mamod.mehyar@gmail.com>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself