search

Home  >  Q&A  >  body text

ruby-on-rails - ruby 怎么打印输出 异常信息的详细消息?

现在有一个测试代码:
def study_ruby_exception arg_1
begin
puts 10/arg_1
rescue Exception => e
puts e.message
end
end
study_ruby_exception 0
当我运行的时候会打印输出:pided by 0 除数为零异常
我想得到这个异常的所有堆栈消息。例如:接防执行这个代码的时候是怎么的调用关系。错在哪个文件的哪 个方法里面等!
我应该怎么输出呢?

PHP中文网PHP中文网2781 days ago1118

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-21 10:58:14

    Exception class has a backtrace method that returns an array containing a stack...

    Reference code...

    def study_ruby_exception arg_1
        begin
            puts 10/arg_1
        rescue Exception => e
            puts e.backtrace
        end
    end
    
    study_ruby_exception 0

    A few words by the way...rescue actually does not have to appear together with begin...

    Your example can actually be written like this...

    def study_ruby_exception arg_1
        puts 10/arg_1
    rescue Exception => e
        puts e.backtrace
    end
    
    study_ruby_exception 0

    reply
    0
  • Cancelreply