【发布时间】:2017-01-02 10:53:08
【问题描述】:
如果错误在数组中,有没有办法在救援子句中将所有错误放入一个数组并从那里调用它们?
例如:
FATAL_ERRORS = %w(Mechanize::ResponseCodeError RestClient::ServiceUnavailable OpenSSL::SSL::SSLError RestClient::BadGateway)
begin
# Do some cool stuff
rescue FATAL_ERRORS => e
puts "Exiting #{e}"
我尝试过的:
我已经尝试从当前线程中抓取错误:
FATAL_ERRORS = Thread.current[:errors] ||= %w(Mechanize::ResponseCodeError RestClient::ServiceUnavailable OpenSSL::SSL::SSLError RestClient::BadGateway)
begin
# Do some cool stuff
rescue FATAL_ERRORS => e
puts "Exiting #{e}"
我也尝试过 splat 运算符:
FATAL_ERRORS = %w(Mechanize::ResponseCodeError RestClient::ServiceUnavailable OpenSSL::SSL::SSLError RestClient::BadGateway)
begin
# Do some cool stuff
rescue *FATAL_ERRORS => e
puts "Exiting #{e}"
splat 和线程都产生以下异常:
rescue in <main>': class or module required for rescue clause (TypeError)
我怎样才能成功地挽救多个错误,而不会将它们全部放在挽救线上并使其看起来很糟糕?
【问题讨论】:
标签: ruby error-handling rescue