On Jul 18, 2010, at 2:06 AM, Kate Yoak wrote:
This is a tiny bit off-topic, but TryCatch is so closely tied to
Moose, this is probably as good a place to ask as any.
I am finally switching away from Error.pm and I am looking to see
how to implement the things I am used to from Error.pm with
TryCatch. It looks to me, there is no magic that will tell me where
in the code the error occurred - I need to implement it myself.
Specifically, I should probably be implementing the throw function
that figures it all out.
I would also need to overload "" to have the code die properly when
the exception is not caught.
Since that's the part Error.pm is actually pretty good at, it's easy
enough. It's just that I am a little confused that this doesn't
exist with TryCatch (I looked into TryCatch::Error and it doesn't do
anything but declare two attributes and has a minor downside that it
doesn't install on my system!)
So really? I know some people here are using it. You guys just do it
from scratch each time? Or am I missing something (as I suspect)
Perhaps you might want to look into using Try::Tiny instead.
TryCatch is very nice syntax and a lot of people have put a lot of
work into it, but in my opinion it is too ambitious. Perhaps this is
because I have been bitten too many times over the years by modules
that try to sugar up exception handling like this. But honestly Perl's
built in exception handling has a lot of issues and quirks to it
already and this module feels like it reaches too far and so my worry
is that it is adding more edge cases to something that already has too
many edge cases.
In short, there is just simply too much code being executed in and
around the exception handling with TryCatch and that doesn't sit well
with me.
Now, I will say that I have not used TryCatch much myself beyond just
testing it out, so I may just be being paranoid, so take my advice
with a GIANT grain of salt.
So the reason I recommend Try::Tiny is because this module are *not*
very ambitious at all, which I believe is (in this context especially)
a very good thing.
Try::Tiny handles the wrapping of a code block and proper trapping of
exceptions within that code block. It does it with a very minimal and
very simple sugar layer that openly acknowledges the fact that it
exists and doesn't try to hide itself from you in stack traces, etc.
Try::Tiny makes serious effort to try and be simple and not to add to
the edge cases and trickiness (see the rejected RT bug about $SIG{DIE}
handling).
In short, Try::Tiny executes the bare minimum of code in and around
the exception handling and is strongly committed to doing so, which
makes me feel much more at ease.
As for exception objects, I am going to echo Yuval's suggestion of
Throwable. Throwable is basically a simple role that represents an
exception and Throwable::Error is a simple implementation of it. Just
like Try::Tiny I think Throwable works really hard to stay within its
realm of responsibility and not spill out of it with extra features
that really don't belong in it. This kind of clarity of purpose is
(IMO) a really good thing when it comes to exception objects.
Anyway, this is all just my opinion, so feel free to ignore it if you
like :)
- Stevan