Afficher un message
Vieux 01/04/2008, 19h49   #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
 
Page generated in 0,05417 seconds with 9 queries