Ruby on rails 如何从特定错误中恢复(Ruby on Rails)
我有一个具体的错误,我想挽救 从控制台抓取的错误是Ruby on rails 如何从特定错误中恢复(Ruby on Rails),ruby-on-rails,ruby,rescue,Ruby On Rails,Ruby,Rescue,我有一个具体的错误,我想挽救 从控制台抓取的错误是 JSON::ParserError: 751: unexpected token at '' 您可以将名称传递给rescue,如下所示: begin # ... rescue JSON::ParserError # ... end 如果要将多个错误类传递给rescue,可以使用逗号将它们分隔开来。可以将名称传递给rescue,如下所示: begin # ... rescue JSON::ParserError # ... e
JSON::ParserError: 751: unexpected token at ''
您可以将名称传递给rescue,如下所示:
begin
# ...
rescue JSON::ParserError
# ...
end
如果要将多个错误类传递给rescue,可以使用逗号将它们分隔开来。可以将名称传递给rescue,如下所示:
begin
# ...
rescue JSON::ParserError
# ...
end
如果要传递多个错误类以进行救援,可以使用逗号分隔这些错误类。您可以捕获不同的错误并对其执行相同的操作或执行不同的操作。语法如下所示 假设您要针对不同的错误执行不同的操作:
begin
# Do something
rescue JSON::ParseError
# Do something when the error is ParseError
rescue JSON::NestingError, JSON::UnparserError
# Do something when the error is NestingError or UnparserError
rescue JSON::JSONError => e
# Do something when the error is JSONError
# Use the error from this variable `e'
rescue # same as rescue StandardError
# Do something on other errors
end
如果要将函数中的所有代码放在begin rescue end块中,则可以将begin end字ommit,而不是编写:
def my_func
begin
# do someting
rescue JSON::ParseError
# handle error
end
end
你可以写信
def my_func
# do something
rescue JSON::ParseError
# handle error
end
记住永远不要从例外中拯救。我知道我的答案对你的问题来说可能有点过于宽泛,但我希望它能帮助你和其他有类似疑问的人。你可以发现不同的错误,对它们采取相同的行动,或者采取不同的行动。语法如下所示 假设您要针对不同的错误执行不同的操作:
begin
# Do something
rescue JSON::ParseError
# Do something when the error is ParseError
rescue JSON::NestingError, JSON::UnparserError
# Do something when the error is NestingError or UnparserError
rescue JSON::JSONError => e
# Do something when the error is JSONError
# Use the error from this variable `e'
rescue # same as rescue StandardError
# Do something on other errors
end
如果要将函数中的所有代码放在begin rescue end块中,则可以将begin end字ommit,而不是编写:
def my_func
begin
# do someting
rescue JSON::ParseError
# handle error
end
end
你可以写信
def my_func
# do something
rescue JSON::ParseError
# handle error
end
记住永远不要从例外中拯救。我知道我的答案对你的问题来说可能有点过于宽泛,但我希望它能帮助你和其他有类似疑问的人