返回是否会在try语句中执行,无论它是否最终阻塞? [英] Does the return will execute in try statement regardless it goes to finally block?

查看:88
本文介绍了返回是否会在try语句中执行,无论它是否最终阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于下面的场景,它会在转到Finally块之前返回R吗?或者直到执行finally块之后再等待它?

这里我的要求是需要在访问我的功能时尽快发送响应

'Getresponse'。请帮助我。

  public  ResponseMsg Getresponse(参数...)
{
ResponseMsg R;
< pre lang = C#>尝试
{
// 检查某些条件
R.Acknowledgement = 1;
返回R;

}
catch (例外)
{
Console.WriteLine( catch);
}
最后
{
// < span class =code-comment>一些Db操作..
}



}

问候,< br $>
Soumya



我的尝试:



  public  ResponseMsg Getresponse(参数...)
{
ResponseMsg R;
< pre lang = C#>尝试
{
// 检查某些条件
R.Acknowledgement = 1;
返回R;

}
catch (例外)
{
Console.WriteLine( catch);
}
最后
{
// < span class =code-comment>一些Db操作..
}



}

解决方案

它将执行 return 语句 - 即它将评估参数 - 然后它将执行 finally 阻止。之后它将调整堆栈并将执行(以及值)返回到调用函数。

如果你不确定,那么创建一些虚拟代码并测试它:

  private   void  myButton_Click(对象发​​件人,EventArgs e)
{
Console.WriteLine( 之前);
Console.WriteLine(ShowMe());
Console.WriteLine( After);
}
私人 int ShowMe()
{
尝试
{
Console.WriteLine( 尝试);
return ShowReturn();
}
最后
{
Console.WriteLine( 最后);
}
}
私有 int ShowReturn()
{
Console.WriteLine( ShowReturn);
return 1 ;
}



您将获得:

 
之前尝试
ShowReturn
最后
1
之后

显示顺序非常好。


finally块也将被执行在代码之间返回Statement。但是从我的观点来看,代码之间的返回通常是糟糕的Praxis。



详情你可以在这里找到:

< a href =https://msdn.microsoft.com/en-us/library/k4hea629(v=vs.100).aspx>尝试...抓住......最后声明 [ ^ ]

Hi,
For the below scenario, will it return R before it goes to Finally block?Or else untill it wait after the finally block is executed?
Here my requirement is need to send the response ASAP while accessing my function
'Getresponse'.Please help me on this.

public ResponseMsg Getresponse(parameters...)
{
   ResponseMsg R;
    <pre lang="C#">try
        {
           //Checking some condition
        R.Acknowledgement = "1";
           Return R;

        }
        catch (Exception)
        {
            Console.WriteLine("catch");
        }
        finally
        {
//           Some Db operations..
        }


}
Regards,
Soumya

What I have tried:

public ResponseMsg Getresponse(parameters...)
{
   ResponseMsg R;
    <pre lang="C#">try
        {
           //Checking some condition
 R.Acknowledgement = "1";
           Return R;

        }
        catch (Exception)
        {
            Console.WriteLine("catch");
        }
        finally
        {
//           Some Db operations..
        }


}

解决方案

It will execute the return statement - i.e. it will evaluate the parameter - and then it will execute the finally block. After that it will adjust the stack and return execution (along with the value) to the calling function.
If you aren't sure, then create some dummy code and test it:

private void myButton_Click(object sender, EventArgs e)
    {
    Console.WriteLine("Before");
    Console.WriteLine(ShowMe());
    Console.WriteLine("After");
    }
private int ShowMe()
    {
    try
        {
        Console.WriteLine("Try");
        return ShowReturn();
        }
    finally
        {
        Console.WriteLine("Finally");
        }
    }
private int ShowReturn()
    {
    Console.WriteLine("ShowReturn");
    return 1;
    }


You will get:

Before
Try
ShowReturn
Finally
1
After

Which shows the order quite nicely.


The finally block will be executed also for the return Statement(s) in between the code. But from my Point of view "Returns" in between the code is a usually bad Praxis.

Details you can find here:
try...catch...finally Statement[^]


这篇关于返回是否会在try语句中执行,无论它是否最终阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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