# Define\_exception gem

**URL:** <https://www.ruby-forum.com/t/define-exception-gem/189222>\
**Category:** Ruby\
**Created:** [May 27, 2010, 1:33am UTC](https://www.ruby-forum.com/t/define-exception-gem/189222 "2010-05-27T01:33:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wes\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@Wes\_B](https://www.ruby-forum.com/u/Wes_B)\
**Post date:** [May 27, 2010, 1:33am UTC](https://www.ruby-forum.com/t/define-exception-gem/189222/1 "2010-05-27T01:33:53Z")

</div>

I’m happy to announce the release of define\_exception 0.0.2. This is the  
initial public release of this library.

= Summary

This gem provides a dry way of defining custom exception in our  
application with default messages so that you don’t have to constantly  
define a message when using raise.

= Typical Exception Definition

It is common in ruby applications to write a custom exception to  
indicate a special error has occurred in your application

```
class MyCustomException < RuntimeError; end #nodoc

```

This can then be invoked by raise with a message

```
raise( MyCustomException, 'Your custom error message here' ) unless 

```

…

If you raise this exception multiple times but want the same error  
message to be presented to the user you have to repeat the above  
statement which is cumbersome. You could define a hash for common error  
messages but this divorces the exception from the message it is to  
deliver.

= A Better Way

A better way of approaching the problem is to define the exception class  
with a default message while preserving the ability to override it using  
raise. This may sound trivial but it is not so straightforward when you  
subclass off of the ruby standard exception classes. This can be done  
easily with this gem using any of the following constructs:

```
class MyClass
  define_exception 'MyTestException', 'This is my default message'
  define_exception :AnotherTestException, 'This is the default 

```

message for another exception’  
define\_exception :yet\_another\_exception, ‘There is always more  
than one way’  
…  
end

Syntactically this has more feel like attr\_accessor and is succinct. The  
first argument is either a string or symbol that defines the name of the  
exception. Usage of the underscore in the symbol name automatically gets  
converted to camel case for the exception name. The second argument is  
the string to define the default message. This allows simple error  
handling to occur repeatedly

```
raise MyTestException unless ...

```

The ability to override the default message is still possible as well

```
raise MyTestException, 'This is a one time error message'

```

= Changing the Parent Class

By default the parent class is RuntimeError. For cases where you need a  
different parent class you can supply the class as an optional third  
argument

```
Class MyClass
  define_exception 'MyTestException', 'This is my default message', 

```

ArgumentError  
…  
end

= Installation

The gem is available from [rubygems.org](http://rubygems.org):

gem install define\_exception

= Usage

```
require 'rubygems'
require 'define_exception'

include DefineException

class MyTest
  define_exception 'MyTestError', 'Test Error Message'
  ...
end

```

= Complete Information

> **[GitHub - wbailey/define\_exception: A simple mixin to make defining custom...](https://github.com/wbailey/define_exception)**
>
> A simple mixin to make defining custom exceptions in ruby classes easier - GitHub - wbailey/define\_exception: A simple mixin to make defining custom exceptions in ruby classes easier

I hope the community finds this useful.

Wes B.
