Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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
def replace_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
  end
end


# Code block 2
def parse
  clean
  replace_markers
end

def replace_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"
      end
    end
  end
end