如何在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

我想打印一个警告,指出错误的类型和消息,而不会向每个救援条款添加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,消息是我的描述

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...