Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
[Mr. Muskrat]: Is it just me or does that timeout issue seems to be happening more often lately?
[Corion]: Mr. Muskrat: I'm not sure if it really happens more often, but I don't exactly know either
[LanX]: yep
[LanX]: more often for some weeks now
[Corion]: I think I'll have to manually (as god) intervene with that node, as the simple reparenting didn't seem to fix the parent/child relationship of the nodes
[Corion]: I think I have an idea but I'll have to open a ticket with on that - hopefully I get to that on the weekend
LanX imagines a burning thorn bush
[Mr. Muskrat]: Thank you!
[Mr. Muskrat]: Oh that is odd. I got the message that it was reparented but yeah, it didn't actually do it. lol
[Corion]: LanX: No, Co-Rion appears as Monkey God :-)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-01-19 16:26 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.