为什么 `rescue Exception => 是不好的风格?e`在Ruby中? [英] Why is it bad style to `rescue Exception => e` in Ruby?

查看:19
本文介绍了为什么 `rescue Exception => 是不好的风格?e`在Ruby中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ryan Davis 的 Ruby QuickRef 说(没有解释):

Ryan Davis’s Ruby QuickRef says (without explanation):

不要拯救异常.曾经.否则我会捅你一刀.

Don’t rescue Exception. EVER. or I will stab you.

为什么不呢?什么是正确的做法?

Why not? What’s the right thing to do?

推荐答案

TL;DR:使用 StandardError 代替一般的异常捕获.当重新引发原始异常时(例如,当救援仅记录异常时),救援 Exception 可能没问题.

TL;DR: Use StandardError instead for general exception catching. When the original exception is re-raised (e.g. when rescuing to log the exception only), rescuing Exception is probably okay.

ExceptionRuby 的异常层次结构的根,所以当你 rescue Exception 你从 everything 中救出,包括子类,例如 SyntaxErrorLoadErrorInterrupt.

Exception is the root of Ruby's exception hierarchy, so when you rescue Exception you rescue from everything, including subclasses such as SyntaxError, LoadError, and Interrupt.

救援Interrupt可以防止用户使用CTRLC退出程序.

Rescuing Interrupt prevents the user from using CTRLC to exit the program.

救援 SignalException 会阻止程序正确响应信号.除了 kill -9 之外,它将无法杀死.

Rescuing SignalException prevents the program from responding correctly to signals. It will be unkillable except by kill -9.

拯救 SyntaxError 意味着失败的 eval 会默默地这样做.

Rescuing SyntaxError means that evals that fail will do so silently.

所有这些都可以通过运行这个程序来显示,并尝试 CTRLCkill 它:

All of these can be shown by running this program, and trying to CTRLC or kill it:

loop do
  begin
    sleep 1
    eval "djsakru3924r9eiuorwju3498 += 5u84fior8u8t4ruyf8ihiure"
  rescue Exception
    puts "I refuse to fail or be stopped!"
  end
end

Exception 中拯救甚至不是默认设置.正在做

Rescuing from Exception isn't even the default. Doing

begin
  # iceberg!
rescue
  # lifeboats
end

不会从 Exception 中拯救,它会从 StandardError 中拯救.您通常应该指定比默认的 StandardError 更具体的内容,但从 Exception 扩大 范围而不是缩小范围,并可能产生灾难性的结果并使寻找错误变得极其困难.

does not rescue from Exception, it rescues from StandardError. You should generally specify something more specific than the default StandardError, but rescuing from Exception broadens the scope rather than narrowing it, and can have catastrophic results and make bug-hunting extremely difficult.

如果您确实想从 StandardError 中进行救援,并且需要一个带有异常的变量,则可以使用以下形式:

If you have a situation where you do want to rescue from StandardError and you need a variable with the exception, you can use this form:

begin
  # iceberg!
rescue => e
  # lifeboats
end

相当于:

begin
  # iceberg!
rescue StandardError => e
  # lifeboats
end

<小时>

Exception 中恢复正常的少数常见情况之一是用于记录/报告目的,在这种情况下,您应该立即重新引发异常:


One of the few common cases where it’s sane to rescue from Exception is for logging/reporting purposes, in which case you should immediately re-raise the exception:

begin
  # iceberg?
rescue Exception => e
  # do some logging
  raise # not enough lifeboats ;)
end

这篇关于为什么 `rescue Exception =&gt; 是不好的风格?e`在Ruby中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆