Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 捕获异常时出现未初始化常量错误_Ruby_Exception - Fatal编程技术网

Ruby 捕获异常时出现未初始化常量错误

Ruby 捕获异常时出现未初始化常量错误,ruby,exception,Ruby,Exception,我需要通过XMLRPC将文章发布到Wordpress并捕获任何异常: connection = XMLRPC::Client.new('mysite.com', '/xmlrpc.php', 80) connection.call( 'metaWeblog.newPost', 1, 'user', 'password', post, true ) 有一个错误: C:/Ruby192/lib/ruby/1.9.1/rexml/parsers/baseparser.rb

我需要通过XMLRPC将文章发布到Wordpress并捕获任何异常:

connection = XMLRPC::Client.new('mysite.com', '/xmlrpc.php', 80)
  connection.call(
  'metaWeblog.newPost',
  1,
  'user',
  'password',
  post,
  true
)
有一个错误:

C:/Ruby192/lib/ruby/1.9.1/rexml/parsers/baseparser.rb:441:in `rescue in pull': #<NoMethodError: undefined method `[]' for nil:NilClass> (REXML::ParseException)
C:/Ruby192/lib/ruby/1.9.1/rexml/parsers/baseparser.rb:341:in `pull'
C:/Ruby192/lib/ruby/1.9.1/rexml/parsers/streamparser.rb:16:in `parse'
C:/Ruby192/lib/ruby/1.9.1/rexml/document.rb:204:in `parse_stream'
C:/Ruby192/lib/ruby/1.9.1/xmlrpc/parser.rb:717:in `parse'
C:/Ruby192/lib/ruby/1.9.1/xmlrpc/parser.rb:460:in `parseMethodResponse'
C:/Ruby192/lib/ruby/1.9.1/xmlrpc/client.rb:421:in `call2'
C:/Ruby192/lib/ruby/1.9.1/xmlrpc/client.rb:410:in `call'
帖子还可以,文章在Wordpress中

接下来,我需要捕捉一个关于站点可用性的异常(当站点不可访问时) 我试图通过以下方式捕捉异常:

connection = XMLRPC::Client.new('mysite.com', '/xmlrpc.php', 80)
begin
  connection.call(
    'metaWeblog.newPost',
    1,
    'user',
    'password',
    post,
    true
  )
rescue REXML::ParseException
  puts "Skipping error"
end
connection = XMLRPC::Client.new('notaccessibleSite.com', '/xmlrpc.php', 80)
begin
  connection.call(
    'metaWeblog.newPost',
    1,
    'user',
    'password',
    post,
    true
  )
rescue REXML::ParseException
  puts "Skipping error"
rescue
  puts "Others errors"
end
但这不起作用:

myscript.rb:47:in `rescue in makeRpc': uninitialized constant Object::REXML (NameError)
from myscript.rb:38:in `makeRpc'
from myscript.rb:62:in `block in postContent'
from myscript.rb:58:in `each'
from myscript.rb:58:in `postContent'
from myscript.rb:71:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
myscript.rb:47:in'rescue in makeRpc':未初始化的常量对象::REXML(NameError)
来自myscript.rb:38:in'makeRpc'
来自myscript.rb:62:in'block in postContent'
来自myscript.rb:58:in'each'
来自myscript.rb:58:in'postContent'
来自myscript.rb:71:in`'
from-e:1:in“load”
from-e:1:in`'

有什么建议吗?

当您测试此案例时,它似乎找不到Object::REXML,可能是
rescue NAME错误
解决了这个问题。

您必须
要求使用“REXML/REXML”
之前。

您是否尝试过
要求使用“REXML/document”
。请查看有关的文档。它需要
'rexml/rexml'
以及
'rexml/parseexception'

以下操作不会产生任何错误:

require "rexml/document"

begin
    doc = REXML::Document.new File.new('blah.txt')
rescue REXML::ParseException => msg
    puts "Failed: #{msg}"
end
但是,如果将
rexml/document
替换为
'rexml/rexml'
,则会得到:

blah.rb:22:in `rescue in <main>': uninitialized constant REXML::ParseException (NameError)
    from abc.rb:20:in `<main>'

我得到未初始化常量Object::REXML或其他(脚本中的第一个)yes,并且
未初始化常量
错误是一个
名称错误
,您可以在rescue语句
rescue NameError
rescue REXML::ParseException
中使用它,并得到
未初始化常量REXML::ParseException(名称错误)
我想要的是:如果
REXML::ParseException
存在,则创建其异常,如果不存在,则为其余部分创建异常。我有
要求“REXML/REXML”
要求“xmlrpc/client”
,不管怎样。第一个代码生成
没有这样的文件或目录-blah.txt(Errno::enoint)
。如何在没有任何错误的情况下捕获这两个异常?
要求“rexml/document”
有效,谢谢。但是有没有办法检查是否存在
REXML::ParseException
?我将更新答案,告诉您如何检查是否定义了
REXML::ParseException
if defined?(REXML::ParseException) == 'constant' && REXML::ParseException.class == Class  
  puts "REXML::ParseException is defined"
else
  puts "REXML::ParseException is NOT defined" 
end