title: Preserve a variable after exception is raised
body: <p>I am working on splitting a source file into tokens, particularly scanning for identifiers. However, there is a requirement that identifiers be AT MOST 30 characters long. When an identifier reaches this length, I raise an exception with the message: <code>'Identifiers can only be 30 characters long, truncating..'</code>. </p>
  
  <p>This is how it should be, but when I raise this exception I jump out my method that scans for identifiers before I am able to store it. I need to somehow raise the exception AND keep the identifier that I have collected so far. Any ideas as to how this could be done?</p>
  
  <pre><code># classify each character, and call approriate scan methods
  def tokenize()
    @infile.each_char do |c|
      begin
        case c
        when /[a-zA-Z\$]/
          scan_identifier(c)
        when /\s/ 
          #ignore spaces
        else
          #do nothing
        end
      rescue TokenizerError =&gt; te
        puts "#{te.class}: #{te.message}"
      end
    end
  end
  
  # Reads an identifier from the source program
  def scan_identifier(id)
    this_id = id #initialize this identifier with the character read above
  
    @infile.each_char do |c|
      if c =~ /[a-zA-Z0-9_]/
        this_id += c 
        # raising this exception leaves this function before collecting the 
        # truncated identifier
        raise TokenizerError, 'Identifiers can only be 30 characters long, truncating..' if this_id.length == 30
      else 
        puts "#{this_id}"
        break # not part of the identifier, or an error
      end
    end
  end
  </code></pre>