# Re: General API design question: FooError vs Foo::Error

**URL:** <https://www.ruby-forum.com/t/re-general-api-design-question-fooerror-vs-foo-error/96429>\
**Category:** Ruby\
**Created:** [June 28, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/re-general-api-design-question-fooerror-vs-foo-error/96429 "2006-06-28T19:26:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [June 28, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/re-general-api-design-question-fooerror-vs-foo-error/96429/1 "2006-06-28T19:26:53Z")

</div>

> FooError to  
> Foo::Error. Take a look at TimeoutError vs Timeout::Error,  
> for example.
> 
> Is there a reason the latter is preferred these days? Just curious.

Any thoughts? Anyone?

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [June 28, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/re-general-api-design-question-fooerror-vs-foo-error/96429/2 "2006-06-28T22:11:37Z")

</div>

On 6/28/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > FooError to  
> > Foo::Error. Take a look at TimeoutError vs Timeout::Error,  
> > for example.
> > 
> > Is there a reason the latter is preferred these days? Just curious.
> 
> Any thoughts? Anyone?
> 
> Dan

My two best guesses would be:

1. shorter code – from within the Timeout class you would have to write

vs.

```
raise Error, "error message"

```

So it could just be fewer keystrokes for the developer. However, I  
think the second guess has more merit.

1. smart use of namespaces

Putting the Error within the Timeout class namespace let’s you quickly  
identify which class defined the method that raised the exception.  
For example

class A  
class Error \< Exception; end  
def a() raise Error, “from #{self.class.name}”  
end

class B \< A  
class Error \< Exception; end  
def b() raise Error, “from #{self.class.name}”  
end

A.new.a  
B.new.a  
B.new.b

# → A::Error: from A

# → A::Error: from B

# → B::Error: from B

So, even though we invoke a() through and instance of B, the exception  
tells us that the error came from one of A’s methods. The only catch  
with this method is if B fails to define its own Error class. Then  
all exceptions will look like they’re coming from A.

Any other thoughts out there?
