请问C#"最后"块总是执行? [英] Does the C# "finally" block ALWAYS execute?

查看:140
本文介绍了请问C#"最后"块总是执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/345091/will-$c$c-in-a-finally-statement-fire-if-i-return-a-value-in-a-try-block\">Will code在最后声明火如果我在Try块返回一个值?

考虑以下code C#code。请问终于块执行?

Consider the following code C# code. Does the "finally" block execute?

public void DoesThisExecute() {
   string ext = "xlsx";
   string message = string.Empty;
   try {
      switch (ext) {
         case "xls": message = "Great choice!"; break;
         case "csv": message = "Better choice!"; break;
         case "exe": message = "Do not try to break me!"; break;
         default:
            message = "You will not win!";
            return;
      }
   }
   catch (Exception) {
      // Handle an exception.
   }
   finally {
      MessageBox.Show(message);
   }
}

哈,我得到了做写这之后,我意识到我可以做在Visual Studio这个测试自己。但是,请随时解答!

Ha, after I got done writing this, I realized I could have done tested this myself in Visual Studio. However, please feel free to answer!

推荐答案

是它:-) <一个href=\"http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx\">http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx

这篇关于请问C#&QUOT;最后&QUOT;块总是执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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