加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在ruby中显示错误类型?

发布时间:2020-12-16 20:18:28 所属栏目:百科 来源:网络整理
导读:在下面的代码中 begin raise StandardError,'message' #some code that raises a lot of exceptionrescue StandardError #handle errorrescue OtherError #handle errorrescue YetAnotherError #handle errorend 我想打印一个警告,指出错误的类型和消息,而不
在下面的代码中
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,消息是我的描述

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读