# Handle ArgumentError to resume loop at next iteration

**URL:** <https://www.ruby-forum.com/t/handle-argumenterror-to-resume-loop-at-next-iteration/86483>\
**Category:** Ruby\
**Created:** [February 22, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/handle-argumenterror-to-resume-loop-at-next-iteration/86483 "2007-02-22T17:43:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [February 22, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/handle-argumenterror-to-resume-loop-at-next-iteration/86483/1 "2007-02-22T17:43:12Z")

</div>

Hi,

still a noob, and still hacking a script to traverse photo directories  
to populate a mysql db with various photo metadata. nearly there, but  
having trouble handling an error from DateTime that dies on a file that  
doesn’t have 3 elements of a civil date.  
can anyone recommend a way of handling this error so that the loop skips  
to the next iteration?  
code below.  
many thanks in advance

===  
…  
find.find(directory) do |path|

next if File.directory?(path)  
next unless path =~ /.jpg$/ or /.JPG$/

puts "trying picture: #{path} "

# get metadata

```
pathName = path
baseName = File.basename(path)
fileSize = File.size(path)
cameraModel = EXIFR::JPEG.new(path).exif.model
picWidth = EXIFR::JPEG.new(path).width
picHeight = EXIFR::JPEG.new(path).height
picDateTaken = EXIFR::JPEG.new(path).date_time_original
DateTime.parse("#{picDateTaken}")
  ^^^error point

mysqlDate = DateTime.parse("#{picDateTaken}")
picShutterSpeed = EXIFR::JPEG.new(path).exposure_time
picAperture = EXIFR::JPEG.new(path).fnumber #returns a fraction as

```

string  
aperture = picAperture.to\_f  
focalLength = EXIFR::JPEG.new(path).focal\_length  
num+= 1

# =\> PRINT

```
puts "added picture: #{pathName} total pics:#{num}"

```

db.query("INSERT INTO JPGS (name, path, size, model, width, height,  
timeStamp, shutterSpeed, aperture, focalLength)  
VALUES (’#{baseName}’, ‘#{pathName}’, ‘#{fileSize}’, ‘#{cameraModel}’,  
‘#{picWidth}’, ‘#{picHeight}’, ‘#{mysqlDate}’, ‘#{picShutterSpeed}’,  
‘#{aperture}’,’#{focalLength}’) ")

end

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [February 22, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/handle-argumenterror-to-resume-loop-at-next-iteration/86483/2 "2007-02-22T18:18:23Z")

</div>

On 2/22/07, jay [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[…]  
begin  
DateTime.parse(picDateTaken)  
rescue ArgumentError  
next  
end  
[…]

-austin
