BC00
BC00

Reputation: 1639

Ruby Exceptions and when they stop things

I am having trouble understanding ruby exceptions and what happens after an exception occurs.

When an exception happens, and I rescue it, do the commands after the exception still get executed, or does it skip them and jump to the rescue? If I want it to do the stuff after the exception what can I do? Thanks!

In the following example:

begin
  var = "string"
  var.do_someting to raise exception
  var.do_something_else
  var.do_something_else_again
rescue => e
  puts "error was #{e}"
end

Upvotes: 0

Views: 60

Answers (1)

bioneuralnet
bioneuralnet

Reputation: 5301

It halts and jumps straight to rescue. If there's stuff that must run no matter what, ensure is probably what you want.

Begin, Rescue and Ensure in Ruby?

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

Upvotes: 4

Related Questions