无法拯救YAML.load异常 [英] Can't rescue YAML.load exception

查看:235
本文介绍了无法拯救YAML.load异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理在Ruby中加载无效的YAML数据,但似乎无法拯救psych引发的异常。



这是一些示例代码来演示我遇到的问题:

  require'yaml'
begin
YAML.load('& *%^ *')
rescue
putsRescued
end

除外:

 #ruby test.rb 
/usr/lib64/ruby/1.9.1/psych.rb:203:in`parse':(<未知>):没有找到预期的字母或数字字符在/ usr / lib64中从/usr/lib64/ruby/1.9.1/psych.rb:203:in`parse_stream'
扫描第1列第1列(Psych :: SyntaxError)
的锚点/ruby/1.9.1/psych.rb:151:in`parse'
from /usr/lib64/ruby/1.9.1/psych.rb:127:in`load'
from test。 rb:3:在`< main>'


解决方案

请参阅开始救援不捕获错误。可以挽救语法错误,但不推荐。这就是为什么你需要跳过额外的键入 rescue SyntaxError


I'm trying to handle loading invalid YAML data in Ruby, but seem to be unable to rescue exceptions raised by psych.

This is some example code to demonstrate the issue I'm having:

require 'yaml'
begin
    YAML.load('&*%^*')
rescue
    puts "Rescued"
end

And the exception:

# ruby test.rb
/usr/lib64/ruby/1.9.1/psych.rb:203:in `parse': (<unknown>): did not find expected alphabetic or numeric character while scanning an anchor at line 1 column 1 (Psych::SyntaxError)
    from /usr/lib64/ruby/1.9.1/psych.rb:203:in `parse_stream'
    from /usr/lib64/ruby/1.9.1/psych.rb:151:in `parse'
    from /usr/lib64/ruby/1.9.1/psych.rb:127:in `load'
    from test.rb:3:in `<main>'

解决方案

See Begin Rescue not catching error. It is possible to rescue Syntax Errors, but not recommended. This is why you need to jump through the extra hoop of typing "rescue SyntaxError".

这篇关于无法拯救YAML.load异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆