Re: When and how to throw exceptions?

2010-04-08 Thread Aristotle Pagaltzis
* Ovid publiustemp-moduleautho...@yahoo.com [2010-04-07 09:05]:
 Presumably the format should try to determine the number of
 conversions in the format and perhaps the alias could generate
 a sub with a corresponding prototype like 'sub
 throw_io_read($$)'. That might give you a touch of compile-time
 safety. Haven't really thought too carefully about this,
 though.

I think the best approach would be to tie String::Formatter into
Exception::Class, specifically its `require_named_input` and
`named_replace` options. To produce its message, an exception
class would pass its declared fields as format inputs. Then
resulting interface would look like this:

use Exception::Class (
'MyException',
'IOOpenRead' = {
isa= 'MyException',
alias  = 'throw_io_open',
fields = [ qw( file mode errno ) ],
format = 'Cannot open %{file} for %{mode}ing: %{errno}',
},
);

You’d use that like so:

open my $fh, '', $filename
or throw_io_open file = $filename, mode = 'read', errno = $!;

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/


Re: When and how to throw exceptions?

2010-04-08 Thread Lutz Gehlen
Hi Ovid,

On Wed, Apr 07, 2010 at 12:04:08AM -0700, Ovid wrote:
 --- On Wed, 7/4/10, Lutz Gehlen lrg...@gmx.net wrote:
  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));
 
 Note, in your code above, you can still have different error
 messages in different packages.  You've made things a touch easier,
 but not really solved the underlying problem.

I usually handle this by having a central module defining the error
functions and offering them for import.

  What about this proposal for Exception::Class?
 
   package My::Exceptions;
 
   use Exception::Class (
   'MyException',
   'IORead' = {
   isa= 'MyException',
   alias  = 'throw_io_read',
   format = [ 'Cannot open %s for reading: %s' ],
   },
   );
 
 And then:
 
   use My::Exceptions 'throw_io_read';
 
   open my $fh, '', $filename or throw_io_read $filename, $!;

I think that an inclusion into Exception::Class is definitely worth
thinking about. I even contacted Dave about it once, but I fear I
wasn't very clear about what I had in mind, so he suggested that I
publish my module first and he would consider it for
Exception::Class. Dave, what do you think about Ovid's suggestion?

One thing that always troubled me when I thought about a good way to
add my desired behaviour to Exception::Class and that is also an
issue with the 'format' suggestion is that I think that an own class
for each message might be a bit of an overkill. If I think of
exception hierarchies I rather think of a class like
My::Exception::IO (instead of IORead in your example). A read
failure would lead to an instance of the IO class with a message
about the read failure.

Therefore I went for a different way just for throwing the
exception. I don't claim to have made up my mind completely about
this. As mentioned before, I started this thread to raise a
discussion. But this is why I did not go your way so far.

Thanks for your comments,
Lutz


Re: When and how to throw exceptions?

2010-04-08 Thread Aristotle Pagaltzis
* Lutz Gehlen lrg...@gmx.net [2010-04-09 01:55]:
 I think that an own class for each message might be a bit of an
 overkill.

Why?

I guess that could be addressed by allowing a hash of formats and
then offering the throwing site to pick one of them by setting
a special field.

The key here is that you want to avoid a situation where catching
code has to parse the error message.

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/