Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Error module

by hotshot (Prior)
on Aug 18, 2003 at 09:56 UTC ( [id://284522]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone!

I'm trying to use Error in my command line utility and had some troubles doing so. For example:
#!/usr/bin/perl -w # filename: test.pl Use Error qe(:try); try { &func1(); print "still in try block\n"; } catch Error::Simple with { my $err = shift; print "Error: ", $err->{'-text'}, "\n"; print "had some problem\n"; } otherwise { print "in otherwise block\n"; } sub func1 { print "in func1\n"; throw Error::Simple('throwing simple error'); }
Several things:
1. The above example doesn't work, I found out later that func1() should be defined BEFORE the try block. Why is that?

2. After I moved the definition of func1() before the try block (now it's working), I still didn't understand how can I return to the try block execution after the catch block finishes, from CPAN:

catch CLASS with BLOCK
BLOCK will be passed two arguments. The first will be the error being thrown. The second is a reference to a scalar variable. If this variable is set by the catch block then, on return from the catch block, try will continue processing as if the catch block was never found.Can anyone help here?

3. If I add at the end of the file a print print "End of program\n";, I get the following error (the program exits after printing the line):

Can't use sting ("1") as a HASHREF while "strict refs" in use at /usr/lib/perl5/site_perl/5.8.0/Error.pm line 456

If I put two print lines at the end, only the first one is printed. As I said I want to put the try/catch in the main loop of my CLI to catch exeptions of command execution, so this problem realy gets me stucked, anyone for this one?

Hotshot

Replies are listed 'Best First'.
Re: Error module
by broquaint (Abbot) on Aug 18, 2003 at 10:15 UTC
    Er, fix your syntax errors and it works fine
    ## lowercase Use, and 'qw' not 'qe' use Error qw(:try); try { &func1(); print "still in try block\n"; } catch Error::Simple with { my $err = shift; print "Error: ", $err->{'-text'}, "\n"; print "had some problem\n"; } otherwise { print "in otherwise block\n"; }; #^ you *must* have a semi-colon as try/catch/etc # are user-defined subroutines. the docs mention this # in the SYNOPSIS sub func1 { print "in func1\n"; throw Error::Simple('throwing simple error'); } __output__ in func1 Error: throwing simple error had some problem

    HTH

    _________
    broquaint

      Thanks a lot, the 'Use' was my typing mistake (in the code I don't have it), but the semi-colon solved the problem.

      Can you refer me to the list of built-in exception I can throw/catch and how can I define a new exception (if possible) and throw/catch it.

      and thanks again

      Hotshot
        Can you refer me to the list of built-in exception I can throw/catch and how can I define a new exception (if possible) and throw/catch it.
        An exception is just anything that calls die with an object, since that is the native perl equivalent of other languages' throw. As for defining new exceptions, it's really just a matter of defining catch and throw methods and calling die with an object in throw. Although even this isn't necessary, but it will keep it in line with current syntax and behaviour of exceptions in the Error module. It would also be a good idea to inherit from either Error or Error::Simple to make your life much simpler.

        Here's an example of a new exception

        { package KaPow; @ISA = 'Error::Simple'; sub throw { warn "Holy thrown exceptions Batman!\n"; ## must die with object die KaPow->new($_[1]); } } use Error ':try'; try { throw KaPow("The Riddler strikes again!"); } catch KaPow with { warn "this just in - $_[0]"; }; __output__ Holy thrown exceptions batman! this just in - The Riddler strikes again! at pmsopw_284549.pl line 9.

        HTH

        _________
        broquaint

Re: Error module
by gjb (Vicar) on Aug 18, 2003 at 10:55 UTC

    To answer the second part of your question: you shouldn't return to the try block once an exception has been thrown. The exception indicates that some abnormal condition has occured (say for instance, a file couldn't be opened), so the rest of the code in the try block should not be executed since it would lead to unpredictable behaviour or simply crash the program.

    Just my 2 cents, -gjb-

    Update: As to dragonchild's remark below: in such a case it's cleaner to work with a finer granularity for the try/catch. I.e. if the caller doesn't consider the exception an error, only the statement generating it should be put in the try/catch construct, no other statements that can be executed regardless of the nature of the exception or after the catch block has done some recovery.

      That's not always true. There are times a module might consider something a failure, but the caller might not. For example, I have a program that uses die to throw signals that aren't exceptions. While I don't have it coded this way, I could easily see using that facility of Error.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Error module
by wirrwarr (Monk) on Aug 18, 2003 at 13:17 UTC
    I still didn't understand how can I return to the try block execution after the catch block finishes, from CPAN:
    catch CLASS with BLOCK
    BLOCK will be passed two arguments. The first will be the error being thrown. The second is a reference to a scalar variable. If this variable is set by the catch block then, on return from the catch block, try will continue processing as if the catch block was never found.Can anyone help here?
    I think you misunderstand what is said there. It doesn't say
    "try will continue as if no error was thrown",
    but it does say
    "try will continue as if no catch block was there".
    that means, if you put ${$_[0]} = 1; (set variable) in your catch block, the otherwise BLOCK will be executed as well, and your script will fail (because the error wasn't caught).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found