# Execution stops once exception is fired

**URL:** <https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426>\
**Category:** Ruby\
**Created:** [January 8, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426 "2008-01-08T23:57:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 8, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/1 "2008-01-08T23:57:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 9, 2008, 2:25am UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/2 "2008-01-09T02:25:37Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 9, 2008, 3:52am UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/3 "2008-01-09T03:52:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 9, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/4 "2008-01-09T19:08:12Z")

</div>

Mauricio Gonzales \<maurigr [gmail.com](http://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

---

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 10, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/5 "2008-01-10T23:55:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deadlykyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@deadlykyo](https://www.ruby-forum.com/u/deadlykyo)\
**Post date:** [January 10, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/execution-stops-once-exception-is-fired/125426/6 "2008-01-10T23:23:10Z")

</div>

> 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
