如何在ruby中显示错误类型?
在下面的代码中
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
我想打印一个警告,说明错误的类型和消息,而不用为每个rescue子句添加print语句,比如
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/25845.html