From: Christopher Dicely Date: 2011-01-26T13:40:36+09:00 Subject: Re: Does Ruby support exception wrapping (exception chaining)? On Tue, Apr 1, 2008 at 10:21 AM, Hartin, Brian wrote: > I could not find any information about this, except regarding DRb. > > It seems like the raise method / Exception class ought to allow me to > pass in a 'causal' exception, e.g. > > begin >  foo > rescue Exception => e >  raise ServiceNotFoundException, "The service could not be contacted", > e > end > > This doesn't work.  I can, however, pass in the backtrace, e.g. > > raise ServiceNotFoundException, "The service could not be contacted", > e.backtrace > > However, doing this I get the stack trace but not the type or message > from the causal exception (unless I manually put them in the message > string).  Ideally, I'd be able to use exception chaining such as in > Java, in which my stack traces include causal exceptions: > > WebServiceCommunicationException: The WSDL could not be obtained from > http://blah/wsdl. >  from (webservice_client.rb):123:in `obtain_wsdl' >  from (webservice_client.rb):12:in `request_price' > caused by ParsingError: Unexpected element 'html' >  from (other_class.rb):234 in 'some_method' >  from (other_class.rb):23 in 'some_other_method' >  ... > > including a 'cause' field on the Exception class, e.g. e.cause. > > Is there some reason why this would not fit the Ruby style? You can certainly do a custom exception class that carries information about a preceding triggering exception; the default handler won't do anything with that information, though, but if you handle the exception yourself, you can include the information with your output. You can even include a method in your custom exception class to dump the info so that client code can just call the method to get the error report. If not, is > this something that would be considered as a patch/change for Ruby? > > I realize I could probably monkeypatch Exception, but this seems (to me) > like it would be an improvement to the language. > > Thanks, > > Brian Hartin > **************************************************************************** > This email may contain material confidential to > Pearson.  If you were not an intended recipient, > please notify the sender and delete all copies. > We may monitor email to and from our network. > **************************************************************************** > >