If I understand you right, you want the possibility to define exception hierarchies _and_ the possibility to have a constant way of throwing exception that is shorter to write.
you can declare classes and derivatives very easily without using Exception::Class simply with the 'package' keyword and 'use parent'. Since the exception classes are just normal perl classes, they can also have member functions. package my_exception_base ; ... package undefined_variable_exception ; use parent my_exception_base ; sub throw_undef { my ($exception_message) = @_ ; ... } if the sub calls are not too expensive in you code you could even have package undefined_variable_exception ; use parent my_exception_base ; use Carp ; sub undefined_exception { my ($is_undefined, $name, $message) = @_ ; unless(defined $is_defined) { croak "$name is not defined. We are $message!\n" ; } } in your code undefined_exception($foo, 'foo', 'doomed') ; But don't throw Exception::Class just yet as it gives you a lot of sugar you may not want to re-implement. Although I haven't looked at its implementation, my guess is that it does create the classes in some way; just pepper those classes with your own subs and you'll be done. my guess is that something like the code below will work. use Exception::Class ( 'MyException', 'AnotherException' => { isa => 'MyException' }, ) ; package AnotherException ; sub throw_another {} ; package wherever_you_want_to_be ; last but not least, a patch to E::C may be accepted by its author. Cheers, Nadim. > 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