Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Exception naming conventions: rely on module name?

Reply
Thread Tools

Exception naming conventions: rely on module name?

 
 
Edwin Fine
Guest
Posts: n/a
 
      12-29-2006
I am wondering about design alternatives regarding exception naming in
Ruby. Is there a naming convention for exceptions that are declared
inside a module?

For example, let's say we have a module named MP3, and we want to have a
base exception for MP3-related errors. There are two basic choices I am
considering here:

module MP3
class MP3Error < StandardError; end # Alternative 1
class Error < StandardError; end # Alternative 2
end

Outside the module, Error would be disambiguated by MP3 (MP3::Error).
However, I am uneasy about this, because of the following situation.
Imagine someone has some existing code that mixes in module Other:

module Other
class Error < StandardError; end
end

class Foo
include Other
def Foo.oops
raise Error, "Who am I?"
rescue Error => e
puts "#{e} #{e.class.name}"
end
end

Foo.oops # Who am I? Other::Error

Now this hapless person mixes in MP3.

class Foo
include Other
include MP3
def Foo.oops
raise Error, "Who am I?"
rescue Error => e
puts "#{e} #{e.class.name}"
end
end

Foo.oops # Who am I? MP3::Error

- Is this a real concern?
- Do you think it would be better to have exception names that are less
likely to clash at the cost of some duplication (e.g. MP3::MP3Error
instead of MP3::Error)?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      12-29-2006
On Dec 29, 2006, at 09:50, Edwin Fine wrote:

> I am wondering about design alternatives regarding exception naming in
> Ruby. Is there a naming convention for exceptions that are declared
> inside a module?
>
> For example, let's say we have a module named MP3, and we want to
> have a base exception for MP3-related errors. There are two basic
> choices I am considering here:
>
> module MP3
> class MP3Error < StandardError; end # Alternative 1
> class Error < StandardError; end # Alternative 2
> end


I typically use #2

> Outside the module, Error would be disambiguated by MP3 (MP3::Error).
> However, I am uneasy about this, because of the following situation.
> Imagine someone has some existing code that mixes in module Other:
>
> module Other
> class Error < StandardError; end
> end
>
> class Foo
> include Other
> def Foo.oops
> raise Error, "Who am I?"
> rescue Error => e
> puts "#{e} #{e.class.name}"
> end
> end
>
> Foo.oops # Who am I? Other::Error


Always fully qualify.

> Now this hapless person mixes in MP3.
>
> class Foo
> include Other
> include MP3
> def Foo.oops
> raise Error, "Who am I?"
> rescue Error => e
> puts "#{e} #{e.class.name}"
> end
> end
>
> Foo.oops # Who am I? MP3::Error
>
> - Is this a real concern?


No. Fully qualify and it won't be a problem.

> - Do you think it would be better to have exception names that are
> less likely to clash at the cost of some duplication (e.g.
> MP3::MP3Error instead of MP3::Error)?


MP3::MP3Error is redundant. There is no problem if you raise
MP3::Error instead. If a consumer of your library decides to omit
Error that's their bug, not yours.

--
Eric Hodel - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!


 
Reply With Quote
 
 
 
 
efine145-nospam01@usa.net
Guest
Posts: n/a
 
      12-30-2006
Eric Hodel wrote:

> MP3::MP3Error is redundant. There is no problem if you raise
> MP3::Error instead. If a consumer of your library decides to omit
> Error that's their bug, not yours.
>
> --
> Eric Hodel - (E-Mail Removed) - http://blog.segment7.net
>
> I LIT YOUR GEM ON FIRE!


Thank you for your advice. I will follow it.
Ed

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we rely on any guarantees based on memory layout and default destructor Fredrik Eriksson C++ 7 10-19-2011 01:58 PM
Can I rely on... Emanuele D'Arrigo Python 6 03-20-2009 01:00 PM
Can I rely on... Emanuele D'Arrigo Python 3 03-19-2009 07:19 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Javax.naming Exception: name not found in naming service. Harman Java 1 07-28-2006 08:51 AM



Advertisments