将code在最后声明火如果我在Try块返回一个值? [英] Will code in a Finally statement fire if I return a value in a Try block?

查看:150
本文介绍了将code在最后声明火如果我在Try块返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回顾了一些code的朋友说,他使用的是内部return语句的try-finally块。请问code在最后一节仍然火即使在try块其余不?

例如:

 公共BOOL的someMethod()
{
  尝试
  {
    返回true;
    抛出新的异常(测试); //似乎并没有得到执行
  }
  最后
  {
    // code问题
  }
}
 

解决方案

简单的回答:是的,

I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block. Does the code in the Finally section still fire even though the rest of the try block doesn't?

Example:

public bool someMethod()
{
  try
  {
    return true;
    throw new Exception("test"); // doesn't seem to get executed
  }
  finally
  {
    //code in question
  }
}

解决方案

Simple answer: Yes.

这篇关于将code在最后声明火如果我在Try块返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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