首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rake异常管理

Rake异常管理
EN

Stack Overflow用户
提问于 2016-01-20 18:50:38
回答 1查看 301关注 0票数 0

考虑一下这个Rake任务:

代码语言:javascript
复制
namespace :foo do
  task :bar do
    begin
      raise 'foo'
    rescue RuntimeError => ex
      raise ex.class, 'bar', ex.backtrace
    end
  end
end

其结果如下:

代码语言:javascript
复制
rake aborted!
bar
/home/vagrant/proj/lib/tasks/foo.rake:52:in `block (2 levels) in <top (required)>'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
foo
/home/vagrant/proj/lib/tasks/foo.rake:52:in `block (2 levels) in <top (required)>'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/vagrant/.gem/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => foo:bar
(See full trace by running task with --trace)

如您所见,既有原始异常(foo),也有新异常(bar)。为什么会这样呢?我原以为foo的例外,已经得到了适当的拯救,不会出现在这里。

EN

回答 1

Stack Overflow用户

发布于 2016-01-20 22:51:51

没有两个原始异常(foo)。

Rakefile:

代码语言:javascript
复制
namespace :foo do
  task :bar do
    begin
      raise "message from foo"
    rescue RuntimeError => ex
      puts "#{ex.message}. foo is rescued"             # <= 1st
      raise ex.class, 'message from bar', ex.backtrace # <= 2nd
    end
  end
end

输出:

代码语言:javascript
复制
message from foo. foo is rescued # <= 1st
rake aborted!
message from bar # <= 2nd
/home/vagrant/Rakefile:4:in `block (2 levels) in <top (required)>'
message from foo # <= from backtrace
/home/vagrant/Rakefile:4:in `block (2 levels) in <top (required)>'
Tasks: TOP => foo:bar
(See full trace by running task with --trace)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34908100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档