例子:CommandLine ::應用程序吞噬我的主要異常,如何避免?
require 'commandline'
class App < CommandLine::Application
def initialize
end
def main
raise 'foo'
end
end
結果
$ ruby test.rb
ERROR: foo
而且這裏的問題開始:在開發過程中總是會有在我的代碼中引發深刻的地方例外,我需要看到的堆棧跟蹤,而不是一些錯位信息。
由於風鈴草,我現在使用此解決方案:
require 'commandline'
class App < CommandLine::Application
def initialize
end
def main
raise 'foo'
rescue Exception => e
puts format_like_real_exception e
end
def format_like_real_exception(e)
s = ''
b = e.backtrace
s << b.shift << ': ' << e.message << "\n"
b.each { |l| s << "\t" << l << "\n" }
s
end
end
當然的格式是沒有必要的,但我更喜歡他們當初要格式化的方式。
這是偉大的,我把它添加到我的Q和格式化了。 – mark 2009-05-23 08:08:48