# Why does the first set of code raise an exception and the second
# doesn't when running on the same test?
# I don't mean why is my code causing errors, I can find that out
# with a debugger and tests, but why would the below begin / end
# block not evaluate the rescue and give me a more descriptive
# exception?
# The first set raises a Hpricot error stating that "string or IO only"
# at the line indicated. So then I wrapped that line in begin / end
# tags to see what is happening at that time and the code no longer
# throws an exception.
# Code block 1
defreplace_markers@text.search("*").each do |element|
if element.class == Hpricot::Comment
field_entry = Field.new(element)
element.swap(field_entry.to_s) # <<== Hpricot Exception Raised
endend# Code block 2
defparse
clean
replace_markers
enddefreplace_markers@text.search("*").each do |element|
if element.class == Hpricot::Comment
field_entry = Field.new(element)
begin
element.swap(field_entry.to_s)
rescue"An error occurred while trying to swap element: #{element}\nWith #{field_entry}\n"endendendend