The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Rich exception hierarchies, multiple inheritance in Ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Rich exception hierarchies, multiple inheritance in Ruby Posted: Mar 21, 2007 6:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Rich exception hierarchies, multiple inheritance in Ruby
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

Rescue clauses are amongst the few things in Ruby that care about the class of your object, so when you do

begin
  ...
rescue Foo
  ...
end

the exception has got to be of kind Foo or a subclass. If rescueing exceptions followed the message sending paradigm, it could have looked like this fictitious snippet:

begin
  ...
rescue foo            # exception object responds to foo
  ...
rescue standard_error # corresponds to the current StandardError

In practice, the fact that rescue cares about the class means that you typically define your exceptions using

MyException = Class.new(StandardError)
# or class MyException < StandardError; end
# more "reload-safe" (but then again, getting a warning in that case can be
# good as you might not notice otherwise)

Indeed, Ruby wants the class used in the rescue clause to be derived from Exception. Since an unqualified rescue handles all StandardError and derived exceptions (but not all Exceptions), you'll often end up subclassing that exception class.

Ruby lacks multiple inheritance, so you cannot have an exception class that derives from e.g. both BackendError and CriticalError, making the following impossible, right?

def foo(*args)
  ...
rescue CriticalError
  # send message to your pager, subsystem shutdown
  # ...
  raise
rescue BackEndError
  # we can handle these locally when not critical...
end


# somewhere else
begin
  foo(*stuff)
rescue BackEndError
  # ...
end

Ruby does have multiple inheritance, it's just that it's been given another name (and it's got a couple restrictions).

You can also use modules to specify which exceptions are to be rescued (ruby-core:10618), allowing you to create rich exception hierarchies which might be useful in some scenario:


Read more...

Read: Rich exception hierarchies, multiple inheritance in Ruby

Topic: A patent was just issued for the Linked List ��� no kidding! Previous Topic   Next Topic Topic: Typo upgrade & cleanup

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use