"异常类/对象预期"哈希对象无法在ruby中获救

Ami*_*ith 3 ruby hash exception-handling

begin
  hash = {"a" => "b"}
  raise hash
rescue Exception => e
  p e.message
end
Run Code Online (Sandbox Code Playgroud)

为什么我没有在结果中获得提升的哈希对象,而是我收到错误 - "异常类/对象预期"

max*_*ner 5

这是关于你引发错误的方式的错误.

当你说raise你需要传递一个"异常类/对象".

一些使用内置错误的示例

raise(StandardError, "my message")

raise ArgumentError, "message"

raise NoMethodError
Run Code Online (Sandbox Code Playgroud)

并创建自定义错误类:

class MyError < StandardError
end

raise MyError, "message"
Run Code Online (Sandbox Code Playgroud)