Execution stops once exception is fired

Hi all,
I am using ruby and watir to test my web application.
The problem is that the ruby script stops the execution each time that
raises an error. Is there any way by which i can continue execution of
the script even after firing exceptions. Here i left some code to
explain the situation

object = Test.new("","")
begin
object.method1()
object.method2() ->here I put an exception intentionally
object.method3()
rescue => e
puts e
end

and never executes the method3()

Thanking you in advance
deadlykyo

Mauricio Gonzales wrote:

object.method3()
rescue => e
puts e
end

and never executes the method3()

That’s the way it’s meant to work, it’s just what exceptions are,
non-local "goto"s to a handler. If you want method3 always to be
called, put it in a “finally” clause.

Clifford H…

Clifford H. wrote:

Mauricio Gonzales wrote:

object.method3()
rescue => e
puts e
end

and never executes the method3()

That’s the way it’s meant to work, it’s just what exceptions are,
non-local "goto"s to a handler. If you want method3 always to be
called, put it in a “finally” clause.

Clifford H…
That’s probably will be a solution but the problem is that i don’t know
when the exception is raised and neither in what method or how many
methods will be, well thx anyway for your help, cya

Mauricio Gonzales <maurigr gmail.com> writes:

That’s probably will be a solution but the problem is that i don’t know
when the exception is raised and neither in what method or how many
methods will be, well thx anyway for your help, cya

You realise that the point of exceptions is to say “Something’s gone
wrong, I
don’t know if I can continue”? If an exception’s been thrown and you
don’t have
the right exception handling code to deal with it, chances are it’s not
right to
continue. That’s the theory anyway

Mauricio Gonzales wrote:

If that is the behavior that you wanted, then you will need to wrap each
method call in begin…rescue block individually.
One way to do it just create a wrapper method to reduce verbosity, like
this:

def wrapper(&block)
begin
block.call
rescue => e
puts e
end
end

wrapper { object.method1 }
wrapper { object.method2 }
wrapper { object.method3 }

I’m sure there are many other way to achieve the same things, such as
using more advance metaprogramming which I’m still not at that level yet
or use aspect oriented technique.

-DJ

You realise that the point of exceptions is to say “Something’s gone
wrong, I
don’t know if I can continue”? If an exception’s been thrown and you
don’t have
the right exception handling code to deal with it, chances are it’s not
right to
continue. That’s the theory anyway

Thx for the explanation, know i’m looking the way to catch the exception
inside the class which invokes the method (class,module or anything
else) who raise the exception, and both are in separeted files, well i
will research about that, thx for your time, cya

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs