Ruby中的異常處理代碼編寫樣本_ruby專題

來源:互聯網
上載者:User

單個異常使用 fail 關鍵字僅僅當捕獲一個異常並且反覆拋出這個異常(因為這裡你不是失敗,而是準確的並且故意拋出一個異常)。

  begin   fail 'Oops'  rescue => error   raise if error.message != 'Oops'  end

    不要為 fail/raise 指定準確的 RuntimeError。

   

 # bad  fail RuntimeError, 'message'  # good - signals a RuntimeError by default  fail 'message'

    寧願提供一個異常類和一條訊息作為 fail/raise 的兩個參數,而不是一個異常執行個體。

   

 # bad  fail SomeException.new('message')  # Note that there is no way to do `fail SomeException.new('message'), backtrace`.  # good  fail SomeException, 'message'  # Consistent with `fail SomeException, 'message', backtrace`.

    不要在 ensure 塊中返回。如果你明確的從 ensure 塊中的某個方法中返回,返回將會優於任何拋出的異常,並且儘管沒有異常拋出也會返回。實際上異常將會靜靜的溜走。

  

 def foo   begin    fail   ensure    return 'very bad idea'   end  end

    Use implicit begin blocks when possible.如果可能使用隱式 begin 代碼塊。

   

 # bad  def foo   begin    # main logic goes here   rescue    # failure handling goes here   end  end  # good  def foo   # main logic goes here  rescue   # failure handling goes here  end

    通過 contingency methods 偶然性方法。 (一個由 Avdi Grimm 創造的詞) 來減少 begin 區塊的使用。

 

  # bad  begin   something_that_might_fail  rescue IOError   # handle IOError  end  begin   something_else_that_might_fail  rescue IOError   # handle IOError  end  # good  def with_io_error_handling    yield  rescue IOError   # handle IOError  end  with_io_error_handling { something_that_might_fail }  with_io_error_handling { something_else_that_might_fail }

    不要抑制異常輸出。

 

  # bad  begin   # an exception occurs here  rescue SomeError   # the rescue clause does absolutely nothing  end  # bad  do_something rescue nil

    避免使用 rescue 的修飾符形式。

   

 # bad - this catches exceptions of StandardError class and its descendant classes  read_file rescue handle_error($!)  # good - this catches only the exceptions of Errno::ENOENT class and its descendant classes  def foo   read_file  rescue Errno::ENOENT => ex   handle_error(ex)  end

    不要用異常來控制流程。

   

 # bad  begin   n / d  rescue ZeroDivisionError   puts "Cannot divide by 0!"  end  # good  if d.zero?   puts "Cannot divide by 0!"  else   n / d  end

    應該總是避免攔截(最頂級的) Exception 異常類。這裡(ruby自身)將會捕獲訊號並且調用 exit,需要你使用 kill -9 殺掉進程。

 

  # bad  begin   # calls to exit and kill signals will be caught (except kill -9)   exit  rescue Exception   puts "you didn't really want to exit, right?"   # exception handling  end  # good  begin   # a blind rescue rescues from StandardError, not Exception as many   # programmers assume.  rescue => e   # exception handling  end  # also good  begin   # an exception occurs here  rescue StandardError => e   # exception handling  end

    將更具體的異常放在救援(rescue)鏈的上方,否則他們將不會被救援。

  # bad  begin   # some code  rescue Exception => e   # some handling  rescue StandardError => e   # some handling  end  # good  begin   # some code  rescue StandardError => e   # some handling  rescue Exception => e   # some handling  end

    在 ensure 區塊中釋放你程式獲得的外部資源。

  

 f = File.open('testfile')  begin   # .. process  rescue   # .. handle error  ensure   f.close unless f.nil?  end

    除非必要, 儘可能使用 Ruby 標準庫中異常類,而不是引入一個新的異常類。(而不是派生自己的異常類)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.