Execution stops once exception is fired

  • Thread starter Mauricio Gonzales
  • Start date
M

Mauricio Gonzales

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
 
C

Clifford Heath

Mauricio said:
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()

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 Heath.
 
M

Mauricio Gonzales

Clifford said:
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 Heath.
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
 
G

Gareth Adams

Mauricio Gonzales said:
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
 
M

Mauricio Gonzales

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
 
W

Wirianto Djunaidi

Mauricio said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top