I tried this earlier and everyone got off on rescue block syntax. Please don't go there. Given the following working code:
begin (1..1000).each do |i| puts i sleep 1 end rescue Exception => e puts "\nCaught exception..." puts "Exception class: #{e.class}" end Pressing CTRL+C while it is running prints out "Caught exception...", as expected. What exactly is going on syntax wise in the rescue line, particularly between Exception and the variable e with the => in between?
The word "rescue" is a keyword... part of the ruby language. "e" is a variable, and could just as functionally be "a", "b", or "c". The following code works just as well.
begin (1..1000).each do |i| puts i sleep 1 end rescue Exception => b puts "\nCaught exception..." puts "Exception class: #{b.class}" end What are "Exception" and "=>"? Is there another way to write this expression to make it more intelligible from a syntactical point of view? I don't think we're dealing with a hash here because the following code compiles but throws an error as soon as CTRL+C is pressed (undefined local variable or method `e').
begin (1..1000).each do |i| puts i sleep 1 end rescue { Exception => b } puts "\nCaught exception..." puts "Exception class: #{b.class}" end Can someone explain what is going on? and specifically what language element '=>' (hashrocket) is in this specific example since it seems to have nothing to do with hashes?