2

I use autodie to handle exceptions from built-ins. Unfortunately its scope is lexical, autodie does not work with methods. Is it possible to throw exceptions from my class methods so that they are handled in the same manner like autodie exceptions. In short, I dont want users of my classes to handle exceptions in two ways - one with the rich exception class that autodie provides for built-ins and another for the simple "die if ..." that my code throws.

If it helps, I use Moose to build my classes

Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43

2 Answers2

2

Looking at the autodie source, I see that it dies using:

    Carp::croak sprintf(
        ERROR_WRONG_FATAL, $Fatal::VERSION, $VERSION, $autodie_path
    );

If you do the same from your code, I would expect the calling code to receive something looking the same.

Edit: As pointed out in comments on this answer, autodie actually dies from within Fatal. A five-minute look over the Fatal source failed to turn up how/where it dies, mostly just making me confused over the relationship between the two modules.[1]

Unless you want to go digging through the Fatal source, your next option would be to define what features of the autodie exceptions are important to you and duplicate them. From the question, it sounds like this could be as simple as identifying the exception class used by autodie and throwing exception objects of that same class.

[1] Fatal has a lot of "use autodie instead because it does this thing that Fatal doesn't" comments, but autodie seems to delegate pretty much everything to Fatal, so it would appear that the things autodie does which Fatal doesn't are still actually done by Fatal...

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • You're looking in the wrong place. That code is what gets executed if `autodie` is unable to load the correct version of the `Fatal` module, which is where the real magic happens. – cjm May 14 '11 at 22:03
  • Thanks. I have temporarily created a subclass of Exception::Class::Base manually and defined attributes similar to autodie::exceptions. Am testing this .... – Gurunandan Bhat May 16 '11 at 08:24
2

If you want your exceptions to be like those in autodie, then I can think of two options:

Use or inherit from the autodie::exception class.

This is pretty straightforward, just perldoc autodie::exception and note which arguments it needs when creating new objects. You might find that inheriting from autodie::exception provides you more flexibility or functionality, in which case you should do that.

Tell autodie to use your own exception class.

This is a little more tricky, but not that hard:

# Inside my/autodie.pm

pacakge my::autodie;
use parent 'autodie';

sub exception_class { return 'my::exception::class'; }

1;

You can then use my::autodie in your code (with lexical scope), but whenever it throws an exception, it will create and throw an object from my::exception::class rather than from autodie::exception. This is probably overkill for what you want, but it's very handy if you want to, say, extend autodie's exceptions to provide stack-backtraces, localisation, or other functionality.

pjf
  • 5,993
  • 25
  • 42