# Having issues with exception

**URL:** <https://www.ruby-forum.com/t/having-issues-with-exception/178104>\
**Category:** Ruby\
**Created:** [October 29, 2009, 3:23am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104 "2009-10-29T03:23:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 29, 2009, 3:23am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/1 "2009-10-29T03:23:14Z")

</div>

Hi, I have been having a bit of an issue. I am trying to create a  
program that will allow me to run a file containing a directory listing  
line by line through a proxy. I am designing this to help with my job, I  
am a web application pen tester. The only issue with the code is I am  
running into 404 and 502 errors which is to be expected. I would like to  
find a way to handle the exception without bringing the execution to a  
halt or deleting anything from the original directory listing. Here is  
my code

begin  
list = IO.foreach(url\_file) {|line| agent.get(line)}  
rescue WWW::Mechanize::ResponseCodeError  
puts “Page not found”  
end

Please if anyone has an idea on how to make this work I would very much  
appreciate the help.

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 29, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/2 "2009-10-29T23:15:32Z")

</div>

Your problem is that you’re catching the exception outside of the loop.  
Try  
something like:

list = IO.foreach(url\_file) do |line|  
begin  
agent.get(line)  
rescue WWW::Mechanize::ResponseCodeError  
puts “Page not found”  
end  
end

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 29, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/3 "2009-10-29T23:32:40Z")

</div>

Judson L. wrote:

> Your problem is that you’re catching the exception outside of the loop.

Should that matter? The extension is still happening after the begin,  
so the rescue should catch it.

> Try  
> something like:
> 
> list = IO.foreach(url\_file) do |line|  
> begin  
> agent.get(line)  
> rescue WWW::Mechanize::ResponseCodeError  
> puts “Page not found”  
> end  
> end

(The inner begin and end are not actually needed here, I think.)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 29, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/4 "2009-10-29T23:33:21Z")

</div>

Marnen Laibow-Koser wrote:

> Judson L. wrote:
> 
> > Your problem is that you’re catching the exception outside of the loop.
> 
> Should that matter? The extension is still happening after the begin,  
> so the rescue should catch it.

Sorry, I meant “exception”, not “extension”, obviously! 😛

[…]

> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 30, 2009, 12:02am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/5 "2009-10-30T00:02:21Z")

</div>

From the OPs description of the problem, and the code presented, yes,  
the  
rescue will catch the exception, but that will stop the loop.

So, if you’re trying to get /missing.html, /broken.html,  
/the\_url\_I\_want.svg, and missing raises a 404, then that will get caught  
by  
the rescue outside of the loop, and you won’t make further requests. In  
other words:

input:  
[http://site.com/missing.html](http://site.com/missing.html)  
[http://site.com/aweseom.html](http://site.com/aweseom.html)

output:  
Page Not Found

When what I understood Kyle to want from the output is something like  
Page Not Found  
“It’s spelled ‘awesome’”

Judson

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 30, 2009, 12:06am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/6 "2009-10-30T00:06:22Z")

</div>

Judson L. wrote:

> From the OPs description of the problem, and the code presented, yes,  
> the  
> rescue will catch the exception, but that will stop the loop.
> 
> So, if you’re trying to get /missing.html, /broken.html,  
> /the\_url\_I\_want.svg, and missing raises a 404, then that will get caught  
> by  
> the rescue outside of the loop, and you won’t make further requests. In  
> other words:
> 
> input:  
> [http://site.com/missing.html](http://site.com/missing.html)  
> [http://site.com/aweseom.html](http://site.com/aweseom.html)
> 
> output:  
> Page Not Found
> 
> When what I understood Kyle to want from the output is something like  
> Page Not Found  
> “It’s spelled ‘awesome’”

I think you’re right about what the OP wants. I had initially thought  
that both syntaxes would stop the loop, but on second reading, I think I  
was wrong about that. Gotcha.

> Judson

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 30, 2009, 7:15am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/7 "2009-10-30T07:15:20Z")

</div>

Kyle Rippee wrote:

> Thank you so much guys. IT WORKS!!! We have seriously been beating our  
> heads over our desks about this. I really appreciate it guys. Do you  
> guys have twitter at all? I would like to give you some credit for this.

Yeah, I’m @marnen (big surprise… 🙂 ).

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![riv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@riv](https://www.ruby-forum.com/u/riv)\
**Post date:** [October 30, 2009, 3:03am UTC](https://www.ruby-forum.com/t/having-issues-with-exception/178104/8 "2009-10-30T03:03:15Z")

</div>

Thank you so much guys. IT WORKS!!! We have seriously been beating our  
heads over our desks about this. I really appreciate it guys. Do you  
guys have twitter at all? I would like to give you some credit for this.
