Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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
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?

Re: Throw customized exception with throwing
by GrandFather (Cardinal) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://665091]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-09-02 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (19 votes), past polls