PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Does Ruby support exception wrapping (exception chaining)?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Does Ruby support exception wrapping (exception chaining)?

Réponse
 
LinkBack Outils de la discussion
Vieux 01/04/2008, 18h21   #1
Hartin, Brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Does Ruby support exception wrapping (exception chaining)?

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
=20 foo
rescue Exception =3D> e
=20 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.
=20 from (webservice_client.rb):123:in `obtain_wsdl'
=20 from (webservice_client.rb):12:in `request_price'
caused by ParsingError: Unexpected element 'html'
=20 from (other_class.rb):234 in 'some_method'
=20 from (other_class.rb):23 in 'some_other_method'
=20 ...

including a 'cause' field on the Exception class, e.g. e.cause.

Is there some reason why this would not fit the Ruby style? 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
************************************************** ***********************=
***=20
This email may contain material confidential to
Pearson. If you were not an intended recipient,=20
please notify the sender and delete all copies.=20
We may monitor email to and from our network.=20
************************************************** ***********************=
***

  Réponse avec citation
Vieux 01/04/2008, 18h49   #2
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does Ruby support exception wrapping (exception chaining)?

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


The second arg to raise (i.e. the message) can be any object:

class ServiceNotFoundException < StandardError; end

begin
begin
raise "foo"
rescue Exception => e
raise ServiceNotFoundException, ["The service could not be contacted",
e]
end
rescue Exception => e_outer
puts e_outer
p e_outer.message
end

__END__

Output:

#<ServiceNotFoundException:0xb7d9b75c>
["The service could not be contacted", #<RuntimeError: foo>]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 02/04/2008, 19h45   #3
Brian Hartin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does Ruby support exception wrapping (exception chaining

Joel,

This doesn't really provide the main benefit to exception wrapping
mechanism: the ability to wrap low-level exceptions in an appropriate
higher-level exception and yet not lose the entire stack trace, or the
types and messages of the low-level exceptions. Exception wrapping
(a.k.a. chaining) in Java (a good model for this feature, I think) makes
the idea of a 'causal Exception' part of the class (via a 'cause'
field), and the stack trace: an Exception's stack trace includes the
causal 'chain' of Exceptions automatically.

I think I can write this up for my own use. I just wondered if I was
missing an existing feature.

Thanks for the response!


Joel VanderWerf wrote:
> Hartin, Brian wrote:
>> end

> The second arg to raise (i.e. the message) can be any object:
>
> class ServiceNotFoundException < StandardError; end
>
> begin
> begin
> raise "foo"
> rescue Exception => e
> raise ServiceNotFoundException, ["The service could not be
> contacted",
> e]
> end
> rescue Exception => e_outer
> puts e_outer
> p e_outer.message
> end
>
> __END__
>
> Output:
>
> #<ServiceNotFoundException:0xb7d9b75c>
> ["The service could not be contacted", #<RuntimeError: foo>]


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

  Réponse avec citation
Vieux 02/04/2008, 19h58   #4
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does Ruby support exception wrapping (exception chaining

Brian Hartin wrote:
> Joel,
>
> This doesn't really provide the main benefit to exception wrapping
> mechanism: the ability to wrap low-level exceptions in an appropriate
> higher-level exception and yet not lose the entire stack trace, or the
> types and messages of the low-level exceptions. Exception wrapping
> (a.k.a. chaining) in Java (a good model for this feature, I think) makes
> the idea of a 'causal Exception' part of the class (via a 'cause'
> field), and the stack trace: an Exception's stack trace includes the
> causal 'chain' of Exceptions automatically.


How about this?

class ServiceNotFoundException < StandardError
attr_reader :cause
def initialize cause
@cause = cause
end
end

begin
begin
raise "foo"
rescue Exception => e
e2 = ServiceNotFoundException.new e
raise e2, "The service could not be contacted: #{e.message}",
e.backtrace
end
rescue Exception => e_outer
puts e_outer
p e_outer.cause
puts e_outer.backtrace.join("\n ")
end

__END__

Output:

The service could not be contacted: foo
#<RuntimeError: foo>
wrapped-exceptions.rb:10


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 02/04/2008, 20h09   #5
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does Ruby support exception wrapping (exception chaining

Joel VanderWerf wrote:
> Brian Hartin wrote:
>> Joel,
>>
>> This doesn't really provide the main benefit to exception wrapping
>> mechanism: the ability to wrap low-level exceptions in an appropriate
>> higher-level exception and yet not lose the entire stack trace, or the
>> types and messages of the low-level exceptions. Exception wrapping
>> (a.k.a. chaining) in Java (a good model for this feature, I think)
>> makes the idea of a 'causal Exception' part of the class (via a
>> 'cause' field), and the stack trace: an Exception's stack trace
>> includes the causal 'chain' of Exceptions automatically.


Here's an alternative, perhaps with too much magic:


class ServiceNotFoundException < StandardError
attr_reader :cause
def initialize cause
@cause = cause
end

DEFAULT_MESSAGE = "The service could not be contacted"

def self.[](e, msg = DEFAULT_MESSAGE)
e2 = new(e)
raise e2, "#{msg}: #{e.message}", e.backtrace
end
end

begin
begin
raise "foo"
rescue Exception => e
ServiceNotFoundException[e]
end
rescue Exception => e_outer
puts e_outer
p e_outer.cause
puts e_outer.backtrace.join("\n ")
end

__END__

Output:

The service could not be contacted: foo
#<RuntimeError: foo>
wrapped-exceptions.rb:17

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12332 seconds with 13 queries