- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我最近开始使用 Ruby 编程,并且正在研究异常处理。
我想知道 ensure
是否等同于 C# 中的 finally
?我应该:
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
rescue
#handle the error here
ensure
file.close unless file.nil?
end
或者我应该这样做吗?
#store the file
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
file.close
rescue
#handle the error here
ensure
file.close unless file.nil?
end
是否确保
无论如何都会被调用,即使没有引发异常?
最佳答案
是的,ensure
确保代码始终被评估。这就是它被称为 ensure
的原因。所以,它相当于Java和C#的finally
。
begin
/rescue
/else
/ensure
/end
的一般流程> 看起来像这样:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
rescue SomeOtherException => some_other_variable
# code that deals with some other exception
else
# code that runs only if *no* exception was raised
ensure
# ensure that this code always runs, no matter what
# does not change the final value of the block
end
您可以省略rescue
、ensure
或else
。您也可以省略变量,在这种情况下您将无法在异常处理代码中检查异常。 (好吧,您始终可以使用全局异常变量来访问最后引发的异常,但这有点老套。)您可以省略异常类,在这种情况下,所有继承自 StandardError< 的异常
将被捕获。 (请注意,这并不意味着捕获了所有 异常,因为有一些异常是Exception
而不是StandardError
的实例。大多数非常危及程序完整性的严重异常,例如 SystemStackError
、NoMemoryError
、SecurityError
、NotImplementedError
、 LoadError
、SyntaxError
、ScriptError
、Interrupt
、SignalException
或 SystemExit
.)
一些 block 形成隐式异常 block 。例如,方法定义隐式也是异常 block ,所以不用写
def foo
begin
# ...
rescue
# ...
end
end
你只写
def foo
# ...
rescue
# ...
end
或
def foo
# ...
ensure
# ...
end
这同样适用于 class
定义和 module
定义。
但是,在您询问的特定情况下,实际上有一个更好的成语。通常,当您使用某些需要在最后清理的资源时,您可以通过将一个 block 传递给一个为您完成所有清理工作的方法来做到这一点。它类似于 C# 中的 using
block ,只是 Ruby 实际上足够强大,您不必等待 Microsoft 的大祭司下山并慷慨地为您更改他们的编译器.在 Ruby 中,您可以自己实现它:
# This is what you want to do:
File.open('myFile.txt', 'w') do |file|
file.puts content
end
# And this is how you might implement it:
def File.open(filename, mode='r', perm=nil, opt=nil)
yield filehandle = new(filename, mode, perm, opt)
ensure
filehandle&.close
end
您知道什么:这已经在核心库中作为 File.open
可用。但它是一种通用模式,您也可以在自己的代码中使用,以实现任何类型的资源清理(在 C# 中使用 using
)或事务或您可能想到的任何其他内容。
唯一不起作用的情况是获取和释放资源分布在程序的不同部分。但如果它是本地化的,如您的示例所示,那么您可以轻松使用这些资源 block 。
顺便说一句:在现代 C# 中,using
实际上是多余的,因为您可以自己实现 Ruby 风格的资源 block :
class File
{
static T open<T>(string filename, string mode, Func<File, T> block)
{
var handle = new File(filename, mode);
try
{
return block(handle);
}
finally
{
handle.Dispose();
}
}
}
// Usage:
File.open("myFile.txt", "w", (file) =>
{
file.WriteLine(contents);
});
关于ruby-on-rails - 在 Ruby 中开始、拯救和确保?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2191632/
双系统grub意外消失怎么办? 不用重装系统、不用去维修店、不会丢数据,教你一招,完美恢复grub! 。 背景 我的电脑是windows和linux双系统,启动项使用的grub。某天
我正在尝试构建一个表,该表将充当第三方服务的批量同步队列。 下面的方法应该不言自明;但需要明确的是,它的目的是向 delayed_syncs 表中添加一个具有 status: :queued 的新可更
我需要在特殊情况下捕获 NameError。但我不想捕获 NameError 的所有子类。有办法实现吗? # This shall be catched begin String::NotExis
我正在使用以下方法发送电子邮件: class Communicate msg end end bcc 包含 4 或 5 个电子邮件地址。 在我的测试中,我注意到两件事: 即使其中一封电子邮件不是
我想知道如何在使用 lib Linear 的 Java API 时挽救所有预测结果。 由于有详细记录,人们可以使用以下代码来挽救预测的准确性: Feature[] instance = new Fea
在我的 Rails 3 应用程序中,我通过以下方式获取 path_info: Rails.application.routes.recognize_path(url, { :method => req
当我将 IO::popen 与不存在的命令一起使用时,我在屏幕上打印了一条错误消息: irb> IO.popen "fakefake" #=> # irb> (irb):1: command
例如,如果我尝试删除一个不存在的表,我将收到以下错误: "# ex if ex.cause.is_a?(PG::UndefinedTable) # do something else
我负责使用 Cucumber 测试非 Rails 网络应用程序。我已经启动并运行了基本测试,I.E.我可以做类似的事情 Then /^the page should have a header$/ d
我有以下表演 Action : def show @name = params[:id] rescue ActiveRecord::RecordNotFound redirect_to :
我是一名优秀的程序员,十分优秀!