I agree with adrianh, your problem is not that the Exceptions aren't been caught -- the problem is that you changed the "API" of your code. You're clients had a contract with you, that in an error/exceptional condition, you would do "foo" (presuably return undef, or false, or something like that) and then all of hte sudden you changed the rules on them, and started throwing exceptions -- so your program dies, which is what it's supposed to do if the exception/error isn't caught.
What you can do, is take advantage of the caller function to determine if a try call is anywhere in the call stack at the moment you encounter an error, and if so then use throw, otherwise just return undef.
If you're paticularly crafty, you can make a subclass of [cpan://Error] that overrides the throw function to do all the work for you, and anytime it encounters an exception with no try in the stack trace, it can not only tell it's caller to return undef, it can log hte full strack trace with args to some special system wide log file which you can audit on a regular basis to track down code paths that don't include exception handling. once you're satisfied that you've done a full migration to the new world order, you can remove your overridden throw function (so that you have an empty subclass) and lose the slight performance penalty.
(This is something Iused to long for in java, a "warn" method that takes a "Throwable" and only throws it if someone upstream wants to catch it -- hence in situations where you are interested in knowing about every instance of MildlyAnoyingButNotCritialException, you can put a try block upstream that catches it, but in the cases where you really don't care you can trust the method you are calling to do the right thing and plod along past whatever situation would normally cause the MildlyAnoyingButNotCritialException)
Update:
While coming home from work, it occured to me that you don't have use caller to walk the stack, there's a simpler way to do it, that gives you finer control over detecting what exceptions people are catching, and which they aren't...
You can use Perl's dynamic scoping, to have a "global" hash keyed on exception types. Then in your new subclass of "Error" override the "try" method to local that hash, and set true values for any Exception types that you have catch clauses for. In your overriden "throw" method, you check that hash, and if the exception you want to throw "isa" any of hte keys in that hash, go ahead and throw it -- otherwise log that the exception would not be caught, so you are defaulting to the old behavior.
In reply to Re: Enforcing exception catching
by hossman
in thread Enforcing exception catching
by dmitri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |