I've built a little script that runs in the background reading and responding to multicast requ开发者_如何学JAVAests, but it seems that on some requests it occasionally crashes. I've been using syslog to have the values of different elements logged to a logfile but the reason for the crashing isn't logged or output because it's running in a separate thread as a background process. Is there any way I can catch the error that's occurring on the app (I do have rescue blocks for the exceptions but there are still issues that aren't being logged) and have it logged somewhere when I'm running it as a background process?
In your app, if you are only rescuing sub-classes of Exception, then there will be some errors that raise Exception
which you will not be capturing.
Use rescue Exception => e
and you should capture everything that raises an Exception or one of its subclasses.
Tim Bray has a nice piece of code to display the Exception hierarchy as it applies to your Ruby and 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
精彩评论