Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to trap errors?

by redss (Monk)
on Jun 23, 2006 at 20:33 UTC ( #557318=perlquestion: print w/replies, xml ) Need Help??

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

I have a program that calls a method from a package, which sometimes makes the program die (all logic & syntax is correct)

How can I make perl trap the error, so that it will continue instead of dying?

Replies are listed 'Best First'.
Re: how to trap errors?
by Zaxo (Archbishop) on Jun 23, 2006 at 20:44 UTC

    eval BLOCK is what you want,

    my $foo = eval { $obj->method(@args) }; warn "Oops! ", $@ if $@;

    After Compline,
    Zaxo

Re: how to trap errors?
by Fletch (Bishop) on Jun 23, 2006 at 20:44 UTC

    See the documentation for eval.

    eval { ## code that may throw errors }; if( $@ ) { ## Handle errors }
Re: how to trap errors?
by shonorio (Hermit) on Jun 23, 2006 at 21:28 UTC
    eval block do a good work for what you wants, but I recomend you to look of Exception classes or Error class at CPAN site. IMO theses classes implements good practices of exception management.

    Solli Moreira Honorio
    Sao Paulo - Brazil
Re: how to trap errors?
by redss (Monk) on Jun 23, 2006 at 21:50 UTC
    thats works great. thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2023-03-30 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (73 votes). Check out past polls.

    Notices?