• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby中的异常处理代码编写示例

Ruby中的异常处理代码编写示例

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了ruby异常处理关键字,ruby,ruby碧玺,ruby woo口红,mac ruby woo口红等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

单个异常使用 fail 关键字仅仅当捕获一个异常并且反复抛出这个异常(因为这里你不是失败,而是准确的并且故意抛出一个异常)。

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

</div>

    不要为 fail/raise 指定准确的 RuntimeError。

   

 # bad
  fail RuntimeError, 'message'

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

</div>

    宁愿提供一个异常类和一条消息作为 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`.

</div>

    不要在 ensure 块中返回。如果你明确的从 ensure 块中的某个方法中返回,返回将会优于任何抛出的异常,并且尽管没有异常抛出也会返回。实际上异常将会静静的溜走。

  

 def foo
   begin
    fail
   ensure
    return 'very bad idea'
   end
  end
</div>

    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

</div>

    通过 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 }

</div>

    不要抑制异常输出。

 

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

  # bad
  do_something rescue nil

</div>

    避免使用 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

</div>

    不要用异常来控制流。

   

 # 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

</div>

    应该总是避免拦截(最顶级的) 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

</div>

    将更具体的异常放在救援(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

</div>

    在 ensure 区块中释放你程式获得的外部资源。

  

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

    除非必要, 尽可能使用 Ruby 标准库中异常类,而不是引入一个新的异常类。(而不是派生自己的异常类)

</div>

您可能想查找下面的文章:

  • ruby 异常处理:rescue
  • Ruby中的异常处理代码编写示例
  • Ruby中处理时间的一些基本操作
  • 详解Ruby中的异常

相关文章

  • ruby元编程之创建自己的动态方法
  • Ruby和Shell脚本实现判断成绩及格功能
  • 深入理解Ruby中的代码块block特性
  • Ruby中的Range对象学习笔记
  • ruby 对象的初始化 方法
  • Ruby中的String对象学习笔记
  • Ruby中require、load、include、extend的区别介绍
  • ruby 杂项
  • ruby实现修改ubuntu下的hosts
  • 艰难完成 nginx + puma 部署 rails 4的详细记录

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Ruby简明教程之循环语句介绍
    • 用Ruby实现一个单元测试框架的教程
    • Windows下Ruby on Rails开发环境安装配置图文教程
    • Ruby环境下安装使用bundler来管理多版本的gem
    • 比较详细的ruby symbol 学习资料
    • Ruby中XML格式数据处理库REXML的使用方法指南
    • Ruby on Rails下的图像处理入门教程
    • Ruby 中$开头的全局变量、内部变量、隐藏变量介绍
    • Ruby中的p和puts的使用区别浅析
    • 在Ruby中查找和执行方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有