好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Ruby中的異常處理代碼編寫示例

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

熱門標簽:地圖標注賺錢項目注冊 常德電銷平臺外呼系統軟件價格 滴滴外呼系統 徐州網絡外呼系統哪個好 湖州u友防封電銷卡 電銷機器人廠商代理 白銀外呼paas系統 百度地圖標注自定義圖片 高德地圖標注客服

單個異常使用 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 標準庫中異常類,而不是引入一個新的異常類。(而不是派生自己的異常類)

您可能感興趣的文章:
  • 詳解Ruby中的異常
  • ruby 異常處理:ensure
  • ruby 異常處理:rescue

標簽:荊門 普洱 梧州 遼寧 張家界 三沙 公主嶺 永州

巨人網絡通訊聲明:本文標題《Ruby中的異常處理代碼編寫示例》,本文關鍵詞  Ruby,中的,異常,處理,代碼,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Ruby中的異常處理代碼編寫示例》相關的同類信息!
  • 本頁收集關于Ruby中的異常處理代碼編寫示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 边吻边摸下面好爽视频完整版| 日韩一级生活片| 黄网站色视频免费老师和学生| 国产福利91精品一区二区三区| 欧美精品电影在线观看| 女人被爽到呻吟的样子 | 欧美精晶无码A片免费看小说| 18禁男女午夜无遮挡网站免费| 国产精品天天看特色大片不卡| 成人国产一区二区三区| 亲嘴扒胸摸屁股激烈下载软件| 无套暴躁白丝秘书| 最近中文字幕免费6| 男男熟睡同性摸硬了| 澳门一级片| 在线国产日韩| 欧美国产日韩综合精品第17| 五月一区二区久久综合天堂| 免费吃奶摸下激烈视频直接看| chinese打桩大学生twink| 久久久久久精品人妻无码| 电影在线日韩电影免费网站| 园产精品无码AV久久不片| 东北老妇女69式XXXX| 欧美双茎同入视频在线观看| 寡妇那牝户又大又肥| 国产牛仔裤在线精品毛片| 女同恋のレズビアンBD在线| 我要看一级黄色片| gogo人体大尺度大胆高清| 翁熄系列乱(老扒)| 国产愉拍aV免费视频一区| 女m跪着趴着调教| 黄色一级片毛片| 一级全黄裸体免费观看视频| 大乳爆乳午夜AV片自慰AI换脸| 久久精品国产一边亲一边做| 熟妇无码欧美XXⅩXX| 亚洲视频网站| 渴望爱火难耐| 一级毛片q片|