MrJaba / simple-exception-definition

A simpler, more DRY way of defining custom exceptions

This URL has Read+Write access

MrJaba (author)
Tue Nov 18 03:31:59 -0800 2008
commit  0f384d8f250bc38b5d11916d10b5c7ea24b961dc
tree    dae2428e3d495a35150cc9c3d7f2c5e999803b18
parent  fe4882e3591cf6ed79a9ec5b82ee9ac6caa99311
README
SimpleExceptionDefinition
=========================

I've found in a few projects I define quite a few custom exceptions, I prefer them as they often express the intent of 
the exception much more clearly and can increase readability.  However, most follow a very similar pattern, which isn't 
very DRY at all, so this plugin goes a little way to help with that.  


Example
=======

class Example

  define_exception :my_custom_exception
  
  def rescue_bad_things
    begin
      do_bad_things
    rescue MyCustomException => e
      logger.error(e.message)
    end
  end
  
  def do_bad_things
    raise MyCustomException.new("Error message")
  end

end

instead of:

class Example
  ...
end

class MyCustomException < StandardError
  def initialize( error_message, object )
    @message = error_message
    @object = object
  end
  
  def message
    @message
  end
end

Saves a few lines at least :)

Thanks
======

Thanks to Simon Jefford for the gemified version and rakefile. 

Copyright (c) 2008 [Tom Crinson], released under the MIT license