Greetings all,
I'm using ruby v1.8 on winxp. I'm trying to get a script together to
delete a particular file if it exists--here it is in full:
···
#------------------------------
# KillOwnerLok.rb
# Looks for Eudora's signal file and tries to delete it.
#
FileToKill = '\\\blackula\Profiles\Laurel\Application
Data\Qualcomm\Eudora\owner.lok'
if File.exist?(FileToKill)
puts FileToKill + " found!"
if File.delete(FileToKill) > 0
puts "Succesfully deleted " + FileToKill + "--you should be able
to use Eudora now."
else
puts "Could not delete " + FileToKill + "for some reason--is
Eudora open?"
end
else
puts "Could not find " + FileToKill
end
# rescue # What would go here?
#------------------------------
[Pls watch for line wrap...]
If I try to run this while the file in question is opened by another
process I get this output:
------------------------------------------
F:\>ruby KillOwnerLok.rb
\\blackula\Profiles\Laurel\Application Data\Qualcomm\Eudora\owner.lok
found!
KillOwnerLok.rb:9:in `delete': Permission denied -
\\blackula\Profiles\Laurel\Ap
plication Data\Qualcomm\Eudora\owner.lok (Errno::EACCES)
from KillOwnerLok.rb:9
------------------------------------------
I'd like to print my "Could not delete..." message in a rescue
section, but I don't know how to translate the error message from the
output into something 'rescuable'. How do I do that?
Thanks!
-Roy
My Guess:
begin
File.delete(filename)
rescue e => Errno::EACCESS
$stderr.puts "Could not delete"
else
$stderr.puts "Successfully deleted"
end
regards,
Brian
···
On Sat, 25 Sep 2004 11:15:33 -0700, Roy Pardee wrote:
Greetings all,
I'm using ruby v1.8 on winxp. I'm trying to get a script together to
delete a particular file if it exists--here it is in full:
#------------------------------
# KillOwnerLok.rb
# Looks for Eudora's signal file and tries to delete it. #
FileToKill = '\\\blackula\Profiles\Laurel\Application
Data\Qualcomm\Eudora\owner.lok'
if File.exist?(FileToKill)
puts FileToKill + " found!"
if File.delete(FileToKill) > 0
puts "Succesfully deleted " + FileToKill + "--you should be able
to use Eudora now."
else
puts "Could not delete " + FileToKill + "for some reason--is
Eudora open?"
end
else
puts "Could not find " + FileToKill
end
# rescue # What would go here?
#------------------------------
-Roy
--
Brian Schröder
http://www.brian-schroeder.de/
Roy Pardee wrote:
If I try to run this while the file in question is opened by another
process I get this output:
------------------------------------------
F:\>ruby KillOwnerLok.rb
\\blackula\Profiles\Laurel\Application Data\Qualcomm\Eudora\owner.lok
found!
KillOwnerLok.rb:9:in `delete': Permission denied -
\\blackula\Profiles\Laurel\Ap
plication Data\Qualcomm\Eudora\owner.lok (Errno::EACCES)
from KillOwnerLok.rb:9
------------------------------------------
I'd like to print my "Could not delete..." message in a rescue
section, but I don't know how to translate the error message from the
output into something 'rescuable'. How do I do that?
Ruby's telling you: Errno::EACCES
rescue Errno::EACCES => exp
puts "Caught an exception: #{exp.message}"
end
But you probably want to catch other Errno's, too. You can catch them all by
rescuing StandardError. If you need to know exactly which exception was
thrown you can call `exp.class' in your rescue clause.
begin
File.delete(filename)
rescue e => Errno::EACCESS
oops, that should have been
rescue Errno::EACCES => e
···
On Sat, 25 Sep 2004 20:32:54 +0200, Brian Schroeder wrote:
$stderr.puts "Could not delete"
else
$stderr.puts "Successfully deleted"
end
regards,
Brian
--
Brian Schröder
http://www.brian-schroeder.de/
Brian Schroeder wrote:
# rescue # What would go here?
My Guess:
begin
File.delete(filename)
rescue e => Errno::EACCESS
Other way round.
(I think the "=>" is read as "to" here.)
rescue Errno::EACCESS => error
$stderr.puts "Could not delete"
else
$stderr.puts "Successfully deleted"
end
regards,
Brian
More regards,
Florian Gross
Thanks all for the responses. I think the exception is not
Errno::EACCES tho. If I throw this at the end of my script:
rescue Errno::EACCESS => exp
puts "In rescue section"
end
Ruby comes back with:
F:\>ruby KillOwnerLok.rb
KillOwnerLok.rb:18: syntax error
rescue Errno::EACCESS => exp
^
I must be doing something very wrong, b/c if I change that to:
rescue StandardError
puts "In rescue section"
end
Ruby says:
F:\>ruby KillOwnerLok.rb
KillOwnerLok.rb:18: syntax error
rescue StandardError
^
Any further advice?
Thanks!
-Roy
Tim Hunter <cyclists@nc.rr.com> wrote in message news:<Wjj5d.7990$zA3.1670742@twister.southeast.rr.com>...
···
Roy Pardee wrote:
> If I try to run this while the file in question is opened by another
> process I get this output:
>
> ------------------------------------------
> F:\>ruby KillOwnerLok.rb
> \\blackula\Profiles\Laurel\Application Data\Qualcomm\Eudora\owner.lok
> found!
> KillOwnerLok.rb:9:in `delete': Permission denied -
> \\blackula\Profiles\Laurel\Ap
> plication Data\Qualcomm\Eudora\owner.lok (Errno::EACCES)
> from KillOwnerLok.rb:9
> ------------------------------------------
>
> I'd like to print my "Could not delete..." message in a rescue
> section, but I don't know how to translate the error message from the
> output into something 'rescuable'. How do I do that?
Ruby's telling you: Errno::EACCES
rescue Errno::EACCES => exp
puts "Caught an exception: #{exp.message}"
end
But you probably want to catch other Errno's, too. You can catch them all by
rescuing StandardError. If you need to know exactly which exception was
thrown you can call `exp.class' in your rescue clause.
Thanks all for the responses. I think the exception is not
Errno::EACCES tho. If I throw this at the end of my script:
rescue Errno::EACCESS => exp
puts "In rescue section"
end
Ruby comes back with:
F:\>ruby KillOwnerLok.rb
KillOwnerLok.rb:18: syntax error
rescue Errno::EACCESS => exp
^
I must be doing something very wrong, b/c if I change that to:
rescue StandardError
puts "In rescue section"
end
Ruby says:
F:\>ruby KillOwnerLok.rb
KillOwnerLok.rb:18: syntax error
rescue StandardError
^
Any further advice?
The rescue keyword can be used in two and a half different ways:
1. As a statement modifier:
puts 0/0 rescue puts 23 # prints 23
2. In a begin...end block:
begin
puts 0/0
rescue ZeroDivisionError => err
puts 42
end # prints 42
2.5. Within other certain supported code blocks:
def foo(bar, baz)
bar/baz
rescue ZeroDivisionError
23
rescue Exception
42
end
If it isn't used in one of these ways, it gives a parse error. I suspect that's what the problem is; you just need to enclose the error-causing part of the script in a begin...end block, with a rescue clause.
cheers,
Mark
···
On Sep 25, 2004, at 9:19 PM, Roy Pardee wrote:
Thanks!
-Roy
Tim Hunter <cyclists@nc.rr.com> wrote in message news:<Wjj5d.7990$zA3.1670742@twister.southeast.rr.com>...
Roy Pardee wrote:
If I try to run this while the file in question is opened by another
process I get this output:
------------------------------------------
F:\>ruby KillOwnerLok.rb
\\blackula\Profiles\Laurel\Application Data\Qualcomm\Eudora\owner.lok
found!
KillOwnerLok.rb:9:in `delete': Permission denied -
\\blackula\Profiles\Laurel\Ap
plication Data\Qualcomm\Eudora\owner.lok (Errno::EACCES)
from KillOwnerLok.rb:9
------------------------------------------
I'd like to print my "Could not delete..." message in a rescue
section, but I don't know how to translate the error message from the
output into something 'rescuable'. How do I do that?
Ruby's telling you: Errno::EACCES
rescue Errno::EACCES => exp
puts "Caught an exception: #{exp.message}"
end
But you probably want to catch other Errno's, too. You can catch them all by
rescuing StandardError. If you need to know exactly which exception was
thrown you can call `exp.class' in your rescue clause.
Bingo--that's it I think. Thanks! It's working now.
That begin ... rescue... end syntax is reminiscient of
try...catch--that's how i'll remember it.
Thanks!
-Roy
Mark Hubbart <discord@mac.com> wrote in message news:<60C1F098-0F84-11D9-B8CF-000A95D2DFAE@mac.com>...
···
On Sep 25, 2004, at 9:19 PM, Roy Pardee wrote:
> Thanks all for the responses. I think the exception is not
> Errno::EACCES tho. If I throw this at the end of my script:
>
> rescue Errno::EACCESS => exp
> puts "In rescue section"
> end
>
> Ruby comes back with:
>
> F:\>ruby KillOwnerLok.rb
> KillOwnerLok.rb:18: syntax error
> rescue Errno::EACCESS => exp
> ^
>
> I must be doing something very wrong, b/c if I change that to:
>
> rescue StandardError
> puts "In rescue section"
> end
>
> Ruby says:
>
> F:\>ruby KillOwnerLok.rb
> KillOwnerLok.rb:18: syntax error
> rescue StandardError
> ^
>
> Any further advice?
The rescue keyword can be used in two and a half different ways:
1. As a statement modifier:
puts 0/0 rescue puts 23 # prints 23
2. In a begin...end block:
begin
puts 0/0
rescue ZeroDivisionError => err
puts 42
end # prints 42
2.5. Within other certain supported code blocks:
def foo(bar, baz)
bar/baz
rescue ZeroDivisionError
23
rescue Exception
42
end
If it isn't used in one of these ways, it gives a parse error. I
suspect that's what the problem is; you just need to enclose the
error-causing part of the script in a begin...end block, with a rescue
clause.
cheers,
Mark