-1

Using begin => rescue => else => ensure => end blocks, doesn't the code after the "end" run no matter what? Couldn't I just put what I would put in "ensure" after "end"?

nikojpapa
  • 33
  • 7

1 Answers1

2

Ensure runs no matter what, even if exceptions are raised in your rescue block.

pixelearth
  • 13,674
  • 10
  • 62
  • 110
  • So, if an exception is raised in the rescue block, the ensure block will run, and then the script will crash? – nikojpapa Jul 23 '15 at 16:57
  • 3
    No, exceptions raised in the rescue must be handled. Separately. The idea is that the ensure block will be run even if the code in the `begin...rescue` terminates prematurely. You often use this to release a resource. – Chris Heald Jul 23 '15 at 17:02