# Efficient way to catch & continue on exception

**URL:** <https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897>\
**Category:** ruby-talk\
**Created:** [19 October 2011 18:49 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897 "2011-10-19T18:49:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kassym\_Dorsel](https://avatars.discourse-cdn.com/v4/letter/k/5fc32e/32.png) [@Kassym\_Dorsel](https://rubytalk.org/u/Kassym_Dorsel)\
**Post date:** [19 October 2011 18:49 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/1 "2011-10-19T18:49:45Z")

</div>

I have the following :

hs = {}  
hs.default = nil  
hs['name3'] = command3  
hs['name2'] = command2  
hs['name1'] = command1

Where command\* are methods that can fail and raise an exception. The  
only way I know to deal with this is :

begin  
hs['name3'] = command3  
rescue  
end  
begin  
hs['name2'] = command2  
rescue  
end  
begin  
hs['name1'] = command1  
rescue  
end

Is there a more compact/nicer of dealing with this. Because I have about  
10-15 of these in a row in my code that I unfortunately can't change to  
anything better.

What I would really want is something that if an exception is raised to  
skip that line and just continue with the next line.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [19 October 2011 19:04 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/2 "2011-10-19T19:04:41Z")

</div>

rbx-head :001 \> def throw  
rbx-head :002?\> throw "hey"  
rbx-head :003?\> end  
=\> #\<Rubinius::CompiledMethod throw file=(irb)\>  
rbx-head :004 \> throw rescue puts "rescued"  
rescued  
=\> nil

---

<div class="post-metadata">

**Author:** ![Carina\_C\_Zona](https://avatars.discourse-cdn.com/v4/letter/c/ea5d25/32.png) [@Carina\_C\_Zona](https://rubytalk.org/u/Carina_C_Zona)\
**Post date:** [19 October 2011 19:14 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/3 "2011-10-19T19:14:05Z")

</div>

> <https://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception>

> **···**
>
> On Wed, Oct 19, 2011 at 11:49 AM, Kassym Dorsel \<k.dorsel@gmail.com\> wrote:
> 
> > begin  
> > hs['name3'] = command3  
> > rescue  
> > end  
> > begin  
> > hs['name2'] = command2  
> > rescue  
> > end  
> > begin  
> > hs['name1'] = command1  
> > rescue  
> > end
> > 
> > Is there a more compact/nicer of dealing with this. Because I have about  
> > 10-15 of these in a row
> 
> --  
> Carina

---

<div class="post-metadata">

**Author:** ![Kassym\_Dorsel](https://avatars.discourse-cdn.com/v4/letter/k/5fc32e/32.png) [@Kassym\_Dorsel](https://rubytalk.org/u/Kassym_Dorsel)\
**Post date:** [19 October 2011 20:38 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/4 "2011-10-19T20:38:59Z")

</div>

That could work. Just one last thing to solve.

assignments = {:name1=\>"arr.grep(/Input:.\*/)[0][6..-1]"}

assignments.each\_pair {|k,v| hs[k] = how do I call this command  
associated with :name1}

.method(:method).call(arguments) and .send("method") only seem to work  
for single methods and not combined ones like the example above.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [19 October 2011 23:41 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/5 "2011-10-19T23:41:24Z")

</div>

A couple options:

hs = {}  
hs['name3'] = (command3 rescue nil)  
hs['name2'] = (command2 rescue nil)  
hs['name1'] = (command1 rescue nil)

Or how about:

module Capture  
&nbsp;&nbsp;def capture(key)  
&nbsp;&nbsp;&nbsp;&nbsp;self[key] = yield  
&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;self[key] = nil  
&nbsp;&nbsp;end  
end

hs = {}.extend Capture  
hs.capture('name3') { command3 }  
hs.capture('name2') { command2 }  
hs.capture('name1') { command1 }

> **···**
>
> On Oct 19, 2011, at 2:49 PM, Kassym Dorsel wrote:
> 
> > I have the following :
> > 
> > hs = {}  
> > hs.default = nil  
> > hs['name3'] = command3  
> > hs['name2'] = command2  
> > hs['name1'] = command1
> > 
> > Where command\* are methods that can fail and raise an exception. The  
> > only way I know to deal with this is :

---

<div class="post-metadata">

**Author:** ![Kassym\_Dorsel](https://avatars.discourse-cdn.com/v4/letter/k/5fc32e/32.png) [@Kassym\_Dorsel](https://rubytalk.org/u/Kassym_Dorsel)\
**Post date:** [19 October 2011 19:45 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/6 "2011-10-19T19:45:46Z")

</div>

C. Zona wrote in post #1027457:

> 

> <https://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception>

Something like that, but I can't use a loop. Each command and each hash  
entry name is different and have to be hard coded.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sam\_Duncan](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sam\_Duncan](https://rubytalk.org/u/Sam_Duncan)\
**Post date:** [19 October 2011 21:46 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/7 "2011-10-19T21:46:03Z")

</div>

Well, you can do this, but remember you have to deal with exceptions thrown in your proc.

ruby-1.9.2-p290 :001 \> hs = {}  
&nbsp;&nbsp;=\> {}  
ruby-1.9.2-p290 :002 \> target =   
&nbsp;&nbsp;=\>   
ruby-1.9.2-p290 :003 \> assignments = {:name1=\>Proc.new {|arr| foo = arr.grep(/Input:.\*/)[0]; foo && foo[6..-1] || nil}}  
&nbsp;&nbsp;=\> {:name1=\>#\<Proc:0x000000009c72a0@(irb):3\>}  
ruby-1.9.2-p290 :004 \> assignments.each\_pair {|k,v| hs[k] = v.call(target)}  
&nbsp;&nbsp;=\> {:name1=\>#\<Proc:0x000000009c72a0@(irb):3\>}  
ruby-1.9.2-p290 :005 \> hs  
&nbsp;&nbsp;=\> {:name1=\>nil}

The proc in assignments could be something like this if you didn't want the foo (you can use tap or something to debug inside the proc if needs be)

ruby-1.9.2-p290 :006 \> assignments = {:name1=\>Proc.new {|arr| arr.grep(/Input:.\*/)[0][6..-1] rescue nil}}  
=\> {:name1=\>#\<Proc:0x00000000872f58@(irb):6\>}  
ruby-1.9.2-p290 :007 \> assignments.each\_pair {|k,v| hs[k] = v.call(target)}  
&nbsp;&nbsp;=\> {:name1=\>#\<Proc:0x00000000872f58@(irb):6\>}  
ruby-1.9.2-p290 :008 \> hs  
&nbsp;&nbsp;=\> {:name1=\>nil}

Does that help?

Sam

> **···**
>
> On 20/10/11 09:38, Kassym Dorsel wrote:
> 
> > That could work. Just one last thing to solve.
> > 
> > assignments = {:name1=\>"arr.grep(/Input:.\*/)[0][6..-1]"}
> > 
> > assignments.each\_pair {|k,v| hs[k] = how do I call this command  
> > associated with :name1}
> > 
> > &nbsp;&nbsp;.method(:method).call(arguments) and .send("method") only seem to work  
> > for single methods and not combined ones like the example above.

---

<div class="post-metadata">

**Author:** ![Sam\_Duncan](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sam\_Duncan](https://rubytalk.org/u/Sam_Duncan)\
**Post date:** [19 October 2011 20:05 UTC](https://rubytalk.org/t/efficient-way-to-catch-continue-on-exception/63897/8 "2011-10-19T20:05:28Z")

</div>

> C. Zona wrote in post #1027457:  
> [Ruby: Continue a loop after catching an exception - Stack Overflow](http://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception)
> 
> Something like that, but I can't use a loop. Each command and each hash  
> entry name is different and have to be hard coded.

Do they need to be hard coded? Can you configure it with a hash of name to command mapping, and then simply iterate the hash with each\_pair?

\> irb  
ruby-1.9.2-p290 :001 \> hs = {}  
&nbsp;&nbsp;=\> {}  
ruby-1.9.2-p290 :002 \> hs.default = nil  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p290 :003 \> def command1; 'command1\_result';end  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p290 :004 \> def command2; 'command2\_result';end  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p290 :005 \> def command3; 'command3\_result';end  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p290 :006 \> assignments = {:name1=\>:command1, :name2=\>:command2, :name3=\>:command3}  
&nbsp;&nbsp;=\> {:name1=\>:command1, :name2=\>:command2, :name3=\>:command3}  
ruby-1.9.2-p290 :007 \> assignments.each\_pair {|k,v| hs[k] = method(v).call}  
&nbsp;&nbsp;=\> {:name1=\>:command1, :name2=\>:command2, :name3=\>:command3}  
ruby-1.9.2-p290 :008 \> hs  
&nbsp;&nbsp;=\> {:name1=\>"command1\_result", :name2=\>"command2\_result", :name3=\>"command3\_result"}

Then you have your loop.

Sam

> **···**
>
> On 20/10/11 08:45, Kassym Dorsel wrote:
