ruby 脚本显示错误但继续
ruby script display errors but continue
我正在尝试 运行 多行完全独立的代码,其中任何一行都可能引发错误。我怎样才能让脚本达到 运行 (有或没有记录错误)直到它不停止地完成?我试图避免在每一行
上使用 begin/rescue
我正在尝试的一些(许多)行 运行:
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id= Steam::User.vanity_to_steamid("MEEE")
badges_hash= Steam::Player.badges(id)
puts "###BADGES###"
puts badges_hash
steam_level=Steam::Player.steam_level(id)
puts "###STEAM LEVEL###"
puts steam_level
....
你可以按照建议在整个街区放一个 begin .. rescue ,或者你也可以这样做:
begin
# one or more lines of code
rescue firstPotentialExecption
# one or lines of code
rescue nextPotentialExecption
# ...
else
# AnyOtherExecptions
end
或者你可以试着弄清楚你在最后得到了什么异常并且只有一个救援语句。
如果不在每一行都使用 begin
/rescue
,我不知道如何解决这个问题。但是,您的示例表明某些行取决于上一行的结果,因此在上一行失败时执行没有意义。此外,基本设置的前几行也可能会引发异常,因为如果没有 id
,文件的其余部分也可能会失败。
您可以将有意义的块包装在 begin
/rescue
块中,然后在失败时向 stderr 打印一条消息(或执行其他操作):
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id = Steam::User.vanity_to_steamid("MEEE")
begin
badges_hash = Steam::Player.badges(id)
puts "###BADGES###"
puts badges_hash
rescue StandardError
$sterr.puts "Failed to get badges"
end
begin
steam_level = Steam::Player.steam_level(id)
puts "###STEAM LEVEL###"
puts steam_level
rescue StandardError
$sterr.puts "Failed to get steam_level"
end
如果文件的其余部分看起来相同,您不妨继续并通过将用户属性的名称存储在数组中并使用 public_send
:[=18 动态请求它们来删除重复项=]
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id = Steam::User.vanity_to_steamid("MEEE")
[
:badges,
:steam_level,
...
].each do |property|
begin
value = Steam::Player.public_send(property, id)
title = property.tr('_', ' ').upcase
puts "####{title}###"
puts value
rescue StandardError
$sterr.puts "Failed to get #{property}"
end
end
我正在尝试 运行 多行完全独立的代码,其中任何一行都可能引发错误。我怎样才能让脚本达到 运行 (有或没有记录错误)直到它不停止地完成?我试图避免在每一行
上使用 begin/rescue我正在尝试的一些(许多)行 运行:
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id= Steam::User.vanity_to_steamid("MEEE")
badges_hash= Steam::Player.badges(id)
puts "###BADGES###"
puts badges_hash
steam_level=Steam::Player.steam_level(id)
puts "###STEAM LEVEL###"
puts steam_level
....
你可以按照建议在整个街区放一个 begin .. rescue ,或者你也可以这样做:
begin
# one or more lines of code
rescue firstPotentialExecption
# one or lines of code
rescue nextPotentialExecption
# ...
else
# AnyOtherExecptions
end
或者你可以试着弄清楚你在最后得到了什么异常并且只有一个救援语句。
如果不在每一行都使用 begin
/rescue
,我不知道如何解决这个问题。但是,您的示例表明某些行取决于上一行的结果,因此在上一行失败时执行没有意义。此外,基本设置的前几行也可能会引发异常,因为如果没有 id
,文件的其余部分也可能会失败。
您可以将有意义的块包装在 begin
/rescue
块中,然后在失败时向 stderr 打印一条消息(或执行其他操作):
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id = Steam::User.vanity_to_steamid("MEEE")
begin
badges_hash = Steam::Player.badges(id)
puts "###BADGES###"
puts badges_hash
rescue StandardError
$sterr.puts "Failed to get badges"
end
begin
steam_level = Steam::Player.steam_level(id)
puts "###STEAM LEVEL###"
puts steam_level
rescue StandardError
$sterr.puts "Failed to get steam_level"
end
如果文件的其余部分看起来相同,您不妨继续并通过将用户属性的名称存储在数组中并使用 public_send
:[=18 动态请求它们来删除重复项=]
Steam.apikey = '8_____1' #http://steamcommunity.com/dev/apikey
$stdout.reopen("playergamedump.csv", "w")
id = Steam::User.vanity_to_steamid("MEEE")
[
:badges,
:steam_level,
...
].each do |property|
begin
value = Steam::Player.public_send(property, id)
title = property.tr('_', ' ').upcase
puts "####{title}###"
puts value
rescue StandardError
$sterr.puts "Failed to get #{property}"
end
end