[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Execution stops once exception is fired

Mauricio Gonzales

1/8/2008 10:58:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Clifford Heath

1/9/2008 1:20:00 AM

0

Mauricio Gonzales wrote:
> 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.

Mauricio Gonzales

1/9/2008 2:52:00 AM

0

Clifford Heath 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 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

--
Posted via http://www.ruby-....

Gareth Adams

1/9/2008 6:08:00 PM

0

Mauricio Gonzales <maurigr <at> 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

1/10/2008 10:23:00 PM

0

> 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
--
Posted via http://www.ruby-....

Wirianto Djunaidi

1/10/2008 10:53:00 PM

0

Mauricio Gonzales wrote:
> Clifford Heath 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 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
>
>
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