Skip to content

Instantly share code, notes, and snippets.

@jackdempsey
Created December 12, 2014 03:15
Show Gist options
  • Save jackdempsey/a78916919aad71133faa to your computer and use it in GitHub Desktop.
Save jackdempsey/a78916919aad71133faa to your computer and use it in GitHub Desktop.
puts "Example 1: No rescue inside loop"
begin
5.times do |i|
begin
p i
raise "loop stops here"
end
end
rescue => e
puts "error caught here: #{e}"
end
puts "\nExample 2: Rescue inside loop"
begin
5.times do |i|
begin
p i
raise "loop stops here, no more looping"
rescue => e
puts "caught here, loop keeps on truckin"
end
end
rescue => e
puts "error caught here: #{e}"
end
@jackdempsey
Copy link
Author

Output:

Example 1: No rescue inside loop
0
error caught here: loop stops here

Example 2: Rescue inside loop
0
caught here, loop keeps on truckin
1
caught here, loop keeps on truckin
2
caught here, loop keeps on truckin
3
caught here, loop keeps on truckin
4
caught here, loop keeps on truckin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment