in reply to Re: Error module
in thread Error module

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

Replies are listed 'Best First'.
Re: Re: Re: Error module
by broquaint (Abbot) on Aug 18, 2003 at 11:45 UTC
    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
        All OO in Perl is done through packages. As all the errors in the Error paradigm are objects, you're going to have to use packages. *shrugs*

        Personally, most of my packages look like:

        package MyErrors::SomeError; use Error::Simple; @ISA = qw(Error::Simple); 1;

        (Yes, I know there's no strict, warnings, or whatever. With something this simple, I tend to skip those things.)

        Then, when you want to use it, you can just catch that error.

        The really neat thing about doing this is that you can create classes of errors. Let's say you have 10 errors that can arise from a database action. Instead of having to handle each one, you can have a base class of MyError::Database and have all your DB errors use it as the base class. MyError::Database would inherit from Error::Simple, thus allowing you to handle all your errors in one place, if you wanted.

        That kind of hierarchy is one of the huge benefits of OO, allowing you to specify where in the hierarchy you want to work in.

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