.NET 相当于 Ruby 的 begin/rescue/else [英] .NET equivalent of Ruby's begin/rescue/else

查看:43
本文介绍了.NET 相当于 Ruby 的 begin/rescue/else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 有一个 else 块可以用于开始/救援(.NET 人员的 try/catch)

Ruby has an else block that would go in a begin/rescue (try/catch for .NET folks)

begin
 #some code
rescue
 #oh noes! Catches errors like catch blocks in .NET
else
 #only executes when NO errors have occured
ensure
 #always executes - just like the finally in .NET
end

else 块中的代码只有在没有出现错误的情况下才会执行..NET 中是否有提供此功能的构造?

The code in the else block will only execute if no errors have been raised. Is there a construct in .NET that provides this functionality?

推荐答案

在.NET中,你可以只列出#some code后的代码:

In .NET, you can just list the code after #some code:

try
{
   // some code
   // Only executes when NO errors have occurred
}
catch (Exception e)
{
    // Catches errors
}
finally
{
    // Always executes
}

//some code 中的任何异常将阻止仅执行"部分发生,因为它将跳转到 catch 然后 finally.

Any exception within // some code will prevent the "Only executes" section from occurring, as it will jump to the catch then finally.

这篇关于.NET 相当于 Ruby 的 begin/rescue/else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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