Exception Raising

Hi.

I have started to introduce the Net::FTP function to download something
from our FTP server, and wanted to introduce error handling.

Code is:

while @try < 6 do
#login to FTP Server, download latest seeding list & upload latest
seeding reports
ftp=Net::FTP.new
ftp.connect(“",21)
ftp.login(user="
*”,password="*********")
ftp.chdir(@remotedirectory)
ftp.get(@[email protected],@seedinglistname,1024)
#upload latest seeding log…
#ftp.put("*********",@remotedirectory+"*********",1024)
ftp.close
puts “FTP activity successful on attempt #{@try}”

rescue Net::FTPError
@try = @try+1
next
end

I get the following error:
syntax error, unexpected keyword_rescue, expecting keyword_end
rescue Net::FTPError

I have copied the syntax from another while loop that is working, so not
sure why this isnt working.

Any help would be appreciated.

Thanks

On Tue, Sep 24, 2013 at 1:00 PM, Graeme H. [email protected]
wrote:

ftp=Net::FTP.new
@try = @try+1
Any help would be appreciated.
Just look at the syntax:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html#S2

Kind regards

robert

Thanks both.

Im quite new to ruby programming, so failing to understand i think… A
bit more information on whats going wrong would help me understand.

In essence, I want the code to run in a loop and if there are any
errors/exceptions, to try again for a max of 5 times before giving up.

I think the part you’re missing is that “begin, rescue, end” blocks are
separate from “while” blocks.

On Tue, Sep 24, 2013 at 1:34 PM, Graeme H. [email protected]
wrote:

Thanks both.

Im quite new to ruby programming, so failing to understand i think… A
bit more information on whats going wrong would help me understand.

It’s purely a syntax issue as the error message indicates.

In essence, I want the code to run in a loop and if there are any
errors/exceptions, to try again for a max of 5 times before giving up.

Did you check link I gave before? I blogged about begin end from a
functional point of view a while ago:
http://blog.rubybestpractices.com/posts/rklemme/003-The_Universe_between_begin_and_end.html

Cheers

robert

Thanks guys, school boy error.

Didn’t have “begin” inside the while loop. Ill now know for next time!

On Sep 24, 2013, at 8:48 AM, Graeme H. [email protected] wrote:

Im using “File.rename @[email protected],
@[email protected]” to move the file…

Take a full read through the File, Dir, and FileUtils classes/modules to
see about working with the file system. Some methods you’ll want to make
note of include exist? directory? writable? and a few others. Do also
look at the Pathname module as is it somewhat of a superset of the
previous 3.

Thanks for your help!


Posted via http://www.ruby-forum.com/.

  1. http://www.ruby-doc.org/core-2.0.0/File.html
  2. http://www.ruby-doc.org/core-2.0.0/Dir.html
  3. http://ruby-doc.com/stdlib-2.0/libdoc/fileutils/rdoc/FileUtils.html
  4. http://ruby-doc.org/stdlib-2.0.0/libdoc/pathname/rdoc/Pathname.html

Guys,

Got another issue, I want to move the local copy of the file its
downloading to a backup folder prior to the FTP download.

Currently, if the file doesn’t exist, then its falling over using
“Errno::ENOENT”.

How do I deal with this? I want the download to continue as it will
create the missing file during the download.

Im using “File.rename @[email protected],
@[email protected]” to move the file…

Thanks for your help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs