How to display error type in ruby?

in the following code

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
end

I want to print a warning stating the type and the message of the error without adding print statement to each of the rescue clauses, like

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
???
 print "An error of type #{???} happened, message is #{???}"
end

begin
  raise ArgumentError, "I'm a description"
rescue Exception => ex
  puts "An error of type #{ex.class} happened, message is #{ex.message}"
end

打印:发生ArgumentError类型的错误,消息是我是一个描述

链接地址: http://www.djcxy.com/p/25846.html

上一篇: Ruby除了MyException之外拯救所有异常

下一篇: 如何在ruby中显示错误类型?