简体   繁体   English

错误消息:并非所有代码路径都返回一个值

[英]Error message: not all code paths return a value

I get this error message in the following line of my code: 我在代码的以下行中收到此错误消息:

public async Task<bool> TestMakePurchase(string productId, string payload)

Error CS0161: 'Game1.TestMakePurchase(string, string)': not all code paths return a value (CS0161) 错误CS0161:“ Game1.TestMakePurchase(字符串,字符串)”:并非所有代码路径都返回值(CS0161)

What does this error message mean and what is wrong with my code? 此错误消息是什么意思,我的代码有什么问题?

Full code: 完整代码:

 async void CheckPurchase()
    {

        bool purchaseIsSuccessful = await TestMakePurchase("com.website.appName.purchaseName", "");          

        if (purchaseIsSuccessful)
        {               

        }
        else
        {

        }
    }


    public async Task<bool> TestMakePurchase(string productId, string payload)
    {
        var billing = CrossInAppBilling.Current;
        try
        {
            var connected = await billing.ConnectAsync();
            if (!connected)
            {                 
                return false;
            }


            var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload);

            if (purchase == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (InAppBillingPurchaseException purchaseEx)
        {

        }

        finally
        {
            await billing.DisconnectAsync();
        }
    }

If the first try catches an exception, it potentially bypasses all of you return statements. 如果第一次try捕获到异常,则可能会绕过您所有的return语句。 You can fix this by adding a return right at the end of your method: 您可以通过在方法末尾添加返回权来解决此问题:

public async Task<bool> TestMakePurchase(string productId, string payload)
{
    var billing = CrossInAppBilling.Current;
    try
    {
        // Snip
    }
    catch (InAppBillingPurchaseException purchaseEx)
    {

    }
    finally
    {
        await billing.DisconnectAsync();
    }

    // Add this line
    return false;
}

If exception occur in try block the code exit without any return value. 如果try块中发生异常,则代码退出,不return任何值。 Please add return statements after finally block. 请在finally阻止之后添加return语句。

You don't have to call return on each conditional statement. 您不必在每个条件语句上都调用return。 Simply track the state with a variable ( purchaseState ). 只需使用变量( purchaseState )跟踪状态即可。 It is unclear what should happen when a InAppBillingPurchaseException is thrown. 目前尚不清楚引发InAppBillingPurchaseException时会发生什么情况。 I would at least log this error. 我至少会记录此错误。

public async Task<bool> TestMakePurchase(string productId, string payload)
{   
    var purchaseSuccesful = false;
    var billing = CrossInAppBilling.Current;
    try
    {                                   
        if (await billing.ConnectAsync())
        {                 
            var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload);
            purchaseSuccesful = purchase != null;
        }
    }
    catch (InAppBillingPurchaseException purchaseEx)
    {
        //log this error?
    }
    finally
    {
        await billing.DisconnectAsync();
    }

    return purchaseSuccesful;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM