Hi Aristotle,
sorry for the delayed reply, I had some computer problems and then 
this topic slipped a bit out of my mind.

On Friday 12 March 2010 23:33:40 Aristotle Pagaltzis wrote:
> * Lutz Gehlen <lrg...@gmx.net> [2010-02-21 01:40]:
> > 1) The first question deals with how to throw exceptions
> > properly. In projects which ask for a more sophisticated way
> > than just carping or croaking I use Exception::Class. However,
> > in both cases the error message has to be assembled when the
> > error is thrown.
> 
> given that Exception::Class already provides a central place to
> define your exception hierarchy along with messages,… I don’t
> follow what it is that you need another central place for?

This might be one point where I don't understand how people use 
Exception::Class or how they throw exceptions at all. As I wrote in 
the original mail I might miss something here.

I understand that I can build a hierarchy of exception classes using 
Exception::Class. But when I throw an exception I still have to 
assemble the error message there, e.g.:

if(!defined($foo)) {
    My::Exception::Class->throw
        ('Variable foo is undefined. We are doomed.');
}

As already shown by this simple example this is likely to span 
several lines thereby drawing unnecessary attention of the reader.
This is mitigated a little bit by the alias parameter of 
Exception::Class. However, if I don't pay close attention I might 
write somewhere else:

if(!defined($bar)) {
    My::Exception::Class->throw
        ('We are doomed because variable bar is undefined.');
}

which is not how it should be. The same kind of error should produce 
the same message.
 
What I need a central place for is the definition of the actual 
error messages. With my module Exception::EasyThrow, I can write at 
the beginning of my module:

use Exception::EasyThrow
    (var_ud => 'Variable %s is undefined. We are doomed.',
     ... => ...);

and then later

var_ud('foo') if(!defined($foo));

which is often possible in one line.
By default, it just croaks the message, but it can be set up to 
throw an Exception::Class object or do something else.

Do you see what I mean? Is this possible with Exception::Class 
alone? Or is it stupid for some reason to want this?

Thanks to everybody for advice
Lutz

Reply via email to