Ruby Exceptions — Why “else”?

前端 未结 5 2057
长情又很酷
长情又很酷 2021-01-31 14:11

I\'m trying to understand exceptions in Ruby but I\'m a little confused. The tutorial I\'m using says that if an exception occurs that does not match any of the exceptions iden

5条回答
  •  情歌与酒
    2021-01-31 14:29

    The only reason I can see for the else block is if you want to execute something before the ensure block when the code in the begin block didn't raise any errors.

    begin
      puts "Hello"
    rescue
      puts "Error"
    else
      puts "Success"
    ensure
      puts "my old friend"
      puts "I've come to talk with you again."
    end
    

提交回复
热议问题