Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Throw customized exception without throwing

by mihirjha (Novice)
on Jan 30, 2008 at 10:19 UTC ( #665091=perlquestion: print w/replies, xml ) Need Help??
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 (Sage) 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"; }


    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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://665091]
Approved by GrandFather
[choroba]: if you didn't know the precision, what output would you expect?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2017-05-24 19:00 GMT
Find Nodes?
    Voting Booth?