# Ignoring Exceptions

**URL:** <https://www.ruby-forum.com/t/ignoring-exceptions/160857>\
**Category:** Ruby\
**Created:** [February 20, 2009, 11:13am UTC](https://www.ruby-forum.com/t/ignoring-exceptions/160857 "2009-02-20T11:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [February 20, 2009, 11:13am UTC](https://www.ruby-forum.com/t/ignoring-exceptions/160857/1 "2009-02-20T11:13:18Z")

</div>

i am using hpricot to parse xml and place it into an object named ‘x’

some\_variable = (x.at(“some\_element\_name”).innerHTML  
if x.at(“some\_element\_name”))

problem is i have a TON of"some\_element\_name" to assign  
and i dont want to do a if check for everyone of them

is there anyway to do a straight

some\_variable = x.at(“some\_element\_name”).innerHTML

and contain them within a begin end and ignore all noMethodExceptions ?  
i am ok with having some\_variable = null

\*noMethodException is raised when i call a .innerHTML method on an  
element that doesnt exist.

if i do a rescue exception , it catches it and thats the end of it , it  
stops executing.

any ideas ?

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [February 20, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/ignoring-exceptions/160857/2 "2009-02-20T12:15:43Z")

</div>

On Feb 20, 11:14 am, Lin Wj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> some\_variable = x.at(“some\_element\_name”).innerHTML
> 
> any ideas ?

Make a function that does the work for you?

def html\_from\_element(x, name)  
elem = x.at(name)  
return elem && elem.innerHTML  
end

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [February 20, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/ignoring-exceptions/160857/3 "2009-02-20T12:19:16Z")

</div>

On Fri, Feb 20, 2009 at 3:44 PM, Lin Wj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i am using hpricot to parse xml and place it into an object named ‘x’  
> […]
> 
> if i do a rescue exception , it catches it and thats the end of it , it  
> stops executing.

I don’t think there’s way to rescue an exception, ignore it and then  
continue execution _at the statement following the one that blew up_.

Two alternatives you might consider:

1. Create a method that does the if x.at … check and call that for  
each variable you need assigned. If you really prefer a rescue over  
the check the method could do that instead.

2. How many variables are we talking about ? I’d consider more than a  
handful of them a code ‘smell’ and think about using a collection. If  
you were looping through a collection then begin/rescue/next would do  
what you’re talking about above.

Cheers,  
lasitha
