4.14.1. begin .. rescue .. else .. ensure .. end
begin # nebo tělo metody
raise "moje výjimka"
puts 'tohle se nespustí'
rescue
puts "nastala výjimka: #{$!}"
ensure
puts 'vždy spušteno'
end
opFile = File.open(opName, "w")
begin
# Exceptions raised by this code will be caught
# by the following rescue clause
while data = socket.read(512)
opFile.write(data)
end
rescue SystemCallError
$stderr.print "IO failed: " + $!
opFile.close
File.delete(opName)
raise
end
begin
eval string
rescue SytaxError, NameError => boom
print "String doesn't compile: " + boom
rescue StandardError => bang
print "Error running script: " + bang
end
f = File.open("testfile")
begin
# .. process
rescue
# .. handle error
ensure
f.close unless f.nil?
end
f = File.open("testfile")
begin
# .. process
rescue
# .. handle error
else
puts "Congratulations -- no errors!"
ensure
f.close unless f.nil?
end