脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Ruby - Ruby中的异常处理代码编写示例

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

2020-05-05 11:27脚本之家 Ruby

这篇文章主要介绍了Ruby中的异常处理代码编写示例,作者对异常处理的方式给出了一些比较通用的建议,需要的朋友可以参考下

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

?
1
2
3
4
5
begin
 fail 'Oops'
rescue => error
 raise if error.message != 'Oops'
end

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

   

?
1
2
3
4
5
# bad
 fail RuntimeError, 'message'
 
 # good - signals a RuntimeError by default
 fail 'message'

    宁愿提供一个异常类和一条消息作为 fail/raise 的两个参数,而不是一个异常实例。

   

?
1
2
3
4
5
6
7
# 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 块中的某个方法中返回,返回将会优于任何抛出的异常,并且尽管没有异常抛出也会返回。实际上异常将会静静的溜走。

  

?
1
2
3
4
5
6
7
def foo
  begin
   fail
  ensure
   return 'very bad idea'
  end
 end

    Use implicit begin blocks when possible.如果可能使用隐式 begin 代码块。

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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 区块的使用。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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 }

    不要抑制异常输出。

 

?
1
2
3
4
5
6
7
8
9
# bad
begin
 # an exception occurs here
rescue SomeError
 # the rescue clause does absolutely nothing
end
 
# bad
do_something rescue nil

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

   

?
1
2
3
4
5
6
7
8
9
# 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

    不要用异常来控制流。

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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 杀掉进程。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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)链的上方,否则他们将不会被救援。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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 区块中释放你程式获得的外部资源。

  

?
1
2
3
4
5
6
7
8
f = File.open('testfile')
 begin
  # .. process
 rescue
  # .. handle error
 ensure
  f.close unless f.nil?
 end

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

延伸 · 阅读

精彩推荐
  • Ruby深入讲解Ruby中Block代码块的用法

    深入讲解Ruby中Block代码块的用法

    这篇文章主要介绍了深入讲解Ruby中Block代码块的用法,block是Ruby学习进阶当中的重要知识,需要的朋友可以参考下 ...

    pringwq4582020-04-27
  • RubyWindows下ruby语言安装教程

    Windows下ruby语言安装教程

    这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下 ...

    脚本之家5522020-04-22
  • RubyRuby编程中关于中断和返回的用法教程

    Ruby编程中关于中断和返回的用法教程

    这篇文章主要介绍了Ruby编程中关于中断和返回的用法教程,作者用代码举例讲解了其中需要注意的问题,需要的朋友可以参考下 ...

    李哲3142020-04-27
  • RubyRuby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails是Ruby世界中一家独大的Web开发框架,要掌握Rails程序的构建,对其目录结构的了解十分必要,下面就来看一下Ruby on Rails所构建的应用程序基本目录结...

    kevinhua4002020-05-09
  • RubyRuby学习笔记之gem 命令详解

    Ruby学习笔记之gem 命令详解

    gem是一种文件组织的包,一般的ruby的很多插件都有由这种各种的包提供。我们来看看gem的用法 ...

    hebedich8662020-04-16
  • RubyRuby中对一元操作符重载实例

    Ruby中对一元操作符重载实例

    这篇文章主要介绍了Ruby中对一元操作符重载实例,实例说明如何对一元操作符进行重载,需要的朋友可以参考下 ...

    junjie1852020-04-14
  • Rubyruby实现修改ubuntu下的hosts

    ruby实现修改ubuntu下的hosts

    本文给大家分享的是通过ruby获取github上的hosts文件内容,修改到本地Ubuntu中,十分的实用,具体你懂得,有需要的小伙伴可以参考下。 ...

    脚本之家3882020-05-03
  • Ruby对Ruby on Rails进行高效的单元测试的教程

    对Ruby on Rails进行高效的单元测试的教程

    这篇文章主要介绍了在Ruby on Rails中进行高效的单元测试的教程,使用到了Ruby的RSpec和Factory Girl框架,需要的朋友可以参考下 ...

    李冠德3562020-04-26