Hello, I was trying to to write a simple ruby program for exceptions. My code is: class ABC begin alpha = ABC.new alpha.foo= [1,2,3] puts foo.inspect.to_s NonExisting raise "all other exceptions" rescue Exception => NoMethodError puts ''Hello'' puts $ERROR_INFO.inspect rescue Exception => NameError puts ''goodbye'' puts $ERROR_INFO.inspect else ensure #pass through and fail naturally end end when i execute this, i always get the output as Hello nil I am unable to understand why the NonExisting is not calling the nameerror. can someone help me with this. Thanx in advance. -- Posted via http://www.ruby-forum.com/.
On Apr 22, 3:19 pm, cutelucks Ms <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> when i execute this, i always get the output as > Hello > nil > > I am unable to understand why the NonExisting is not calling the > nameerror. > can someone help me with this. >presumably because calling foo= is raising a NoMethodError because there is no such method. Fred> Thanx in advance. > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On Apr 22, 3:19�pm, cutelucks Ms <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt> > wrote: > >> when i execute this, i always get the output as >> Hello >> nil >> >> I am unable to understand why the NonExisting is not calling the >> nameerror. >> can someone help me with this. >> > presumably because calling foo= is raising a NoMethodError because > there is no such method. > > Fredbut even when i comment the intial code and start from ''NonExisting'' line the same output comes. I guess it should now have gone to nameerror rite but still the output is hello nil -- Posted via http://www.ruby-forum.com/.
On Apr 22, 3:52 pm, cutelucks Ms <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote:> but even when i comment the intial code and start from ''NonExisting'' > line the same output comes. I guess it should now have gone to nameerror > rite but still the output is > hello > nilIf you''ve commented out some bits and not others I can only guess as to what your code actually is. Fred> -- > Posted viahttp://www.ruby-forum.com/.
On Apr 22, 3:52 pm, cutelucks Ms <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > but even when i comment the intial code and start from ''NonExisting'' > line the same output comes. I guess it should now have gone to nameerror > rite but still the output is > hello > nilOh, and you rescue clause is wrong too - you''re rescuing all Exceptions, not just NoMethodErrors (and then it''s probably trying to assign the exception object to the NoMethodError constant or something crazy like that). Check whatever ruby reference you''re using for the correct syntax Fred> -- > Posted viahttp://www.ruby-forum.com/.