Ruby Exceptions — Why “else”?

前端 未结 5 2058
长情又很酷
长情又很酷 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:36

    Here's a concrete use-case for else in a begin expression. Suppose you're writing automated tests, and you want to write a method that returns the error raised by a block. But you also want the test to fail if the block doesn't raise an error. You can do this:

    def get_error_from(&block)
      begin
        block.call
      rescue => err
        err  # we want to return this
      else
        raise "No error was raised"
      end
    end
    

    Note that you can't move the raise inside the begin block, because it'll get rescued. Of course, there are other ways without using else, like checking whether err is nil after the end, but that's not as succinct.

    Personally, I rarely use else in this way because I think it's rarely needed, but it does come in handy in those rare cases.

    EDIT

    Another use case occurred to me. Here's a typical begin/rescue:

    begin
      do_something_that_may_raise_argument_error
      do_something_else_when_the_previous_line_doesnt_raise
    rescue ArgumentError => e
      handle_the_error
    end
    

    Why is this less than ideal? Because the intent is to rescue when do_something_that_may_raise_argument_error raises ArgumentError, not when do_something_else_when_the_previous_line_doesnt_raise raises.

    It's usually better to use begin/rescue to wrap the minimum code you want to protect from a raise, because otherwise:

    • you may mask bugs in the code that wasn't supposed to raise
    • the intention of rescue is harder to decipher. Someone (including your future self) may read the code and wonder "Which expression did I want to protect? It looks like expression ABC... but maybe expression DEF too???? What was the author intending?!" Refactoring becomes much more difficult.

    You avoid those problems with this simple change:

    begin
      do_something_that_may_raise_argument_error
    rescue ArgumentError => e
      handle_the_error
    else
      do_something_else_when_the_previous_line_doesnt_raise
    end
    

提交回复
热议问题