我已经构建了一个在后台运行的小脚本读取和响应多播请求,但似乎在某些请求中它偶尔会崩溃。我一直在使用syslog将不同元素的值记录到日志文件中,但是没有记录或输出崩溃的原因,因为它作为后台进程在单独的线程中运行。有什么方法可以捕获应用程序上发生的错误(我确实有异常的救援块,但仍然存在未记录的问题)并在我将其作为后台进程运行时将其记录在某处?
答案 0 :(得分:1)
在你的应用中,如果你只是在拯救Exception的子类,那么会出现一些你不会捕获的Exception
错误。
使用rescue Exception => e
,您应捕获引发异常或其子类的所有内容。
Tim Bray有一段很好的代码来显示异常层次结构,因为它适用于你的Ruby和gems:
exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls.ancestors.include? Exception
next if exceptions.include? cls
next if cls.superclass == SystemCallError # avoid dumping Errno's
exceptions << cls
cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end
indent = 0
tree_printer = Proc.new do |t|
t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
space = (' ' * indent); space ||= ''
puts space + k.to_s
indent += 2; tree_printer.call t[k]; indent -= 2
end
end
tree_printer.call tree