0

In Perl I handle errors with:

eval{
     };
if($@) {
     }

In Ruby I have used:

 begin
 rescue Exception => e
 sleep 2
 end 

Is this correct way to do in Ruby, and will this work if the Internet or a server goes down? If the above is wrong are there any ways of doing it in Ruby similar to Perl?

jam
  • 3,640
  • 5
  • 34
  • 50
user2996524
  • 59
  • 1
  • 7

1 Answers1

2

If you need to rescue from a possible exception, you got it right. You have to:

begin
  # do some useful but dangerious work
rescue StandardError => e
  # something went wrong, try to work around it;
  # object "e" containts usefull error information
ensure
  # anyway, cleanup after doing what you've started
end

P.S. If the server goes down literally (e.g. the hardware is off) – no exception code-handling will help you out.

P.P.S. The Internet probably won't go down anytime soon.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99