How to display error type in ruby?
在以下代码中
1 2 3 4 5 6 7 8 9 10 | 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语句,例如
1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
1 2 3 4 5 | begin raise ArgumentError,"I'm a description" rescue Exception => ex puts"An error of type #{ex.class} happened, message is #{ex.message}" end |
prints:发生了类型argumenterror的错误,消息是I'm a description