将code在最后声明火如果我在Try块返回一个值?我在、声明、code、Try

2023-09-02 21:08:10 作者:影子少年

我回顾了一些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?

安卓版ChatGPT下周上线 OpenAI补全AI帝国最后一块拼图

Example:

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

解决方案

Simple answer: Yes.