문제

In C#.NET, let's take the following example

[WebMethod]
public int TakeAction()
{
    try {
        //Call method A
        Return 1;
    } catch (Exception e) {
        //Call method B
        Return 0;
    } finally {
        //Call method C
    }
}

Now let's say method C is a long running process.

Does the client who invokes TakeAction get back the return value, before method C is invoked, or after it is invoked / completed?

도움이 되었습니까?

해결책

The return value is evaluated first, then the finally block executes, then control is passed back to the caller (with the return value). This ordering is important if the expression for the return value would be changed by the finally block. For example:

Console.WriteLine(Foo()); // This prints 10

...

static int Foo()
{
    int x = 10;
    try
    {
        return x;
    }
    finally
    {
        // This executes, but doesn't change the return value
        x = 20;
        // This executes before 10 is written to the console
        // by the caller.
        Console.WriteLine("Before Foo returns");
    }
}

다른 팁

anything in finally block is executed after leaving try block. In your case it either returns 1 or 0 and then executes method c. for more info on try-catch-finally you can refer this

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top