最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby中的異常處理代碼編寫示例

 更新時間:2015年08月03日 12:15:41   投稿:goldensun  
這篇文章主要介紹了Ruby中的異常處理代碼編寫示例,作者對異常處理的方式給出了一些比較通用的建議,需要的朋友可以參考下

單個異常使用 fail 關(guān)鍵字僅僅當捕獲一個異常并且反復(fù)拋出這個異常(因為這里你不是失敗,而是準確的并且故意拋出一個異常)。

  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 的兩個參數(shù),而不是一個異常實例。

   

 # 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 塊中的某個方法中返回,返回將會優(yōu)于任何拋出的異常,并且盡管沒有異常拋出也會返回。實際上異常將會靜靜的溜走。

  

 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 創(chuàng)造的詞) 來減少 begin 區(qū)塊的使用。

 

  # 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

    應(yīng)該總是避免攔截(最頂級的) Exception 異常類。這里(ruby自身)將會捕獲信號并且調(diào)用 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 區(qū)塊中釋放你程式獲得的外部資源。

  

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

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

相關(guān)文章

  • 詳解Ruby中的代碼塊及其參數(shù)傳遞

    詳解Ruby中的代碼塊及其參數(shù)傳遞

    block代碼塊基本上是開始學習Ruby后的第一個坎,接下來就帶大家詳解Ruby中的代碼塊及其參數(shù)傳遞,需要的朋友可以參考下
    2016-05-05
  • 詳解Ruby中的異常

    詳解Ruby中的異常

    這篇文章主要介紹了詳解Ruby中的異常,異常的拋出和處理是各種編程語言學習中基礎(chǔ)知識,需要的朋友可以參考下
    2015-04-04
  • 淺談Ruby on Rails下的rake與數(shù)據(jù)庫數(shù)據(jù)遷移操作

    淺談Ruby on Rails下的rake與數(shù)據(jù)庫數(shù)據(jù)遷移操作

    Rails中的Migration相對來說更適合做數(shù)據(jù)庫的對象集合操作,而自動化的rake則是一個較好的選擇,下面來淺談Ruby on Rails下的rake與數(shù)據(jù)庫數(shù)據(jù)遷移操作,需要的朋友可以參考下
    2016-06-06
  • 冒泡排序算法及Ruby版的簡單實現(xiàn)

    冒泡排序算法及Ruby版的簡單實現(xiàn)

    冒泡排序為最基本的排序算法之一,其時間復(fù)雜度為O(n^2),這里我們就來簡單看一下冒泡排序算法及Ruby版的簡單實現(xiàn),首先還是先來了解算法原理:
    2016-05-05
  • Ruby中使用設(shè)計模式中的簡單工廠模式和工廠方法模式

    Ruby中使用設(shè)計模式中的簡單工廠模式和工廠方法模式

    這篇文章主要介紹了Ruby中使用設(shè)計模式中的簡單工廠模式和工廠方法模式的示例,這兩種模式經(jīng)常被用于Ruby on Rails開發(fā)的結(jié)構(gòu)設(shè)計中,需要的朋友可以參考下
    2016-03-03
  • Ruby3多線程并行Ractor使用方法詳解

    Ruby3多線程并行Ractor使用方法詳解

    這篇文章主要介紹了Ruby3多線程并行Ractor使用方法詳解,Ruby3之前使用Thread來創(chuàng)建新線,Ruby3通過Ractor支持真正的多線程并行,多個Ractor之間可并行獨立運行程,需要的朋友可以參考下
    2022-04-04
  • 在Ruby中利用Net::SMTP類發(fā)送電子郵件的教程

    在Ruby中利用Net::SMTP類發(fā)送電子郵件的教程

    這篇文章主要介紹了在Ruby中利用Net::SMTP類發(fā)送電子郵件的教程,包括類中所帶方法的用法介紹,需要的朋友可以參考下
    2015-05-05
  • Ruby基礎(chǔ)知識之數(shù)據(jù)類型

    Ruby基礎(chǔ)知識之數(shù)據(jù)類型

    這篇文章主要介紹了Ruby基礎(chǔ)知識之數(shù)據(jù)類型,本文講解了數(shù)值類型、字符串類型、字符類型、哈希類型、范圍類型、對象標識、對象的類、類型等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • 使用C++來編寫Ruby程序擴展的教程

    使用C++來編寫Ruby程序擴展的教程

    這篇文章主要介紹了使用C++來編寫Ruby程序擴展的教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • Ruby on Rails遷移時的一些注意事項

    Ruby on Rails遷移時的一些注意事項

    這篇文章主要介紹了Ruby on Rails遷移時的一些注意事項,包括建議的使用change方法取代up與down方法等細節(jié),需要的朋友可以參考下
    2015-08-08

最新評論

社会| 南澳县| 英吉沙县| 车致| 托克托县| 通山县| 茂名市| 项城市| 鄯善县| 文安县| 洪泽县| 九龙城区| 洛隆县| 河西区| 武宁县| 错那县| 靖边县| 昌邑市| 永平县| 岐山县| 尉氏县| 普宁市| 苍梧县| 普陀区| 隆子县| 芜湖县| 凌源市| 吐鲁番市| 沽源县| 苏尼特右旗| 江口县| 武邑县| 霸州市| 垫江县| 靖边县| 扎囊县| 土默特左旗| 霍城县| 建德市| 金沙县| 岚皋县|