help with exceptions

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.

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

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 nil

If you've commented out some bits and not others I can only guess as to what your code actually is.

Fred

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 nil

Oh, 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