tags:

views:

41

answers:

1

Local Variable

begin
  transaction  #Code inside transaction 
    object = Class.new attributes
    raise unless object.save!
  end 
rescue
  puts object.error.full_messages # Why can't we use local varible inside rescue ?
end

Instance Variable

begin
  transaction  #Code inside transaction 
    @object = Class.new attributes
    raise unless @object.save!
  end 
rescue
  puts @object.error.full_messages # This is working fine.
end
+4  A: 

You most certainly can access local variables defined in a begin, in the corresponding rescue block (assuming of course, the exception has been raised, after the variable was set).

What you can't do is access local variables that are defined inside a block, outside of the block. This has nothing to do with exceptions. See this simple example:

define transaction() yield end
transaction do
  x = 42
end
puts x # This will cause an error because `x` is not defined here.

What you can do to fix this, is to define the variable before the block (you can just set it to nil) and then set it inside the block.

x = nil
transaction do
  x = 42
end
puts x # Will print 42

So if you change your code like this, it will work:

begin
  object = nil
  transaction do  #Code inside transaction 
    object = Class.new attributes
    raise unless object.save!
  end 
rescue
  puts object.error.full_messages # Why can't we use local varible inside rescue ?
end
sepp2k
@sepp2k I have update my question. Can you please help with it ?
krunal shah
@krunal: I updated my answer.
sepp2k