Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Re: Re: Error module

by hotshot (Prior)
on Aug 18, 2003 at 16:22 UTC ( [id://284628]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Error module
in thread Error module

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

Replies are listed 'Best First'.
Re5: Error module
by dragonchild (Archbishop) on Aug 18, 2003 at 17:07 UTC
    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.

      I'm not too familiar with OO programing in perl, so a little question that may seem foolish. In the example you gave:
      package MyErrors::SomeError; use Error::Simple; @ISA = qw(Error::Simple); 1;
      Do I need to put these lines in a .pl file and include it (use or package?)in my program in order to be able to catch these errors, or is there another way?

      Hotshot
        Please help yourself and read up on Perl OO. There are very good books on the matter, one written by our very own Damian Conway. There is also a good section in the Llama book, by our very own Randal Schwartz.

        The minimum you'd need to use what I gave you would be to put those lines in a file called SomeError.pm which would be located in a directory called MyErrors. The directory MyErrors should be in the same directory as your Perl script. (You can actually put it anywhere, but it's easiest to just put it there.)

        In your Perl script, you put use MyErrors::SomeError; right after the use Error qw(:try); line.

        Good luck! If you try this and end up with errors, please post your script and what errors you're getting. That way, we can best help you.

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

        it seems from your reply that it's not just oo perl that you're unfamiliar with, but modules in general. take a look at perlmod and perlmodlib for module information (including some oo info.) to study up on oo perl, perltoot and perltooc are good tutorials.

        ~Particle *accelerates*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found