mihirjha has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! How can we throw customized exception without throwing the exception itself like we do with Fatal? Regards, Mihir
  • Comment on Throw customized exception without throwing

Replies are listed 'Best First'.
Re: Throw customized exception with throwing
by GrandFather (Saint) on Jan 30, 2008 at 10:34 UTC
    use strict; use warnings; my %exceptions = (cust1 => \&cust1Handler, cust2 => \&cust2Handler); eval { die "cust1\n"; }; if ($@) { chomp $@; die "Unhandled exception: $@\n" unless exists $exceptions{$@}; $exceptions{$@}->(); } else { print "Success\n"; } sub cust1Handler { print "Turned to custard - 1\n"; } sub cust2Handler { print "Turned to custard - 2\n"; }

    Prints:

    Turned to custard - 1

    may be what you are after. Note the newline magic to suppress the context text that die would otherwise add.


    Perl is environmentally friendly - it saves trees
Re: Throw customized exception with throwing
by moritz (Cardinal) on Jan 30, 2008 at 10:31 UTC
    What is your question? How Fatal does its magic? If yes, look in the source code.

    Or is your question if you can use Fatal for non-builtin functions? (if yes, just try it - I think it should work)

    Note that Fatal does throw exceptions, though it's perhaps not obvious from the code that uses fatalized functions.

    And what exactly do you mean with "customized exceptions"? Exception Objects? Or just different error messages?