单个异常使用 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"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 标准库中异常类,而不是引入一个新的异常类。(而不是派生自己的异常类)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《阿瑞亚之旅》评测:更适合佛系玩家体质的跑团
- 黑神话悟空浮屠界紫门怎么开 浮屠界紫门打开方法介绍
- 黑神话悟空龟泪怎么获得 黑神话悟空龟泪获得方法介绍
- 黑神话悟空怎么获得还魂丹 黑神话悟空还魂丹获取方法一览
- HiFi顶级人声天碟《试音齐秦》wav
- 张敬轩.2012-WhyNot(EP)【环球】【WAV+CUE】
- 张小英1988-情愁[新加坡复黑版][WAV+CUE]
- 游民金币礼品补货啦 Steam充值卡等你兑换!
- 《暗喻幻想:ReFantazio》M站均分94分 近年最好的RPG游戏之一
- 今年第一项诺贝尔奖揭幕 生理学或医学奖揭晓
- 《梁静茹 华语乐坛情歌天后 爱久见人心》[WAV/分轨][360MB]
- 《田震 绝版珍藏 依然在飞》[WAV+CUE][600MB]
- 《60首经典金曲 4CD》[WAV/分轨][1.4GB]
- 赵传.1988-我很丑,可是我很温柔【滚石】【WAV+CUE】
- 遗忘曲线.2024-遗忘曲线(EP)【LuuvLabel】【FLAC分轨】