in reply to Error module

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

Replies are listed 'Best First'.
Re: Re: Error module
by hotshot (Prior) on Aug 18, 2003 at 11:15 UTC
    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

        I don't have packages, I just want to throw an exception with an error message from the functions that will be caught by the main loop of my prog, if I'll use Error::Simple as follows:
        sub func { ... throw Error::Simple('some message'); } try { func(); } catch Error::Simple with { print "caught something\n"; }
        it will catch all errors including perl errors such as Undifined subrutine..., etc, and I don't want that of course.
        Is there a simple way to do that (without using packages)?

        Hotshot