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!