不是所有的code路径,而使用try,catch和finally返回一个值 [英] not all code paths return a value while using try, catch and finally

查看:224
本文介绍了不是所有的code路径,而使用try,catch和finally返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行下面的函数,我得到一个错误:

When I execute the following function, I get an error:

错误Excel1.WebServiceFunctions.CreateMyTask():   不是所有的code路径返回一个值

"Error 'Excel1.WebServiceFunctions.CreateMyTask()': not all code paths return a value

我的code:

public int CreateMyTask()
{
    try
    {
        Console.WriteLine("Invoking CreateTask method");
        Console.WriteLine("-----------------------------------");
        m_taskID = taskClient.CreateTask(m_tInstance);
        Console.WriteLine("Task create successfully:ID=" + m_taskID.ToString());
        Console.WriteLine("-----------------------------------");
        return m_taskID;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An exception has occured. Please check the Excel sheet for more info" + ex);
    }
    finally
    {
        GC.Collect();
    }
}

我怎么能走这背后的任何想法?我做了一些谷歌搜索,但我不能为零完全相同

Any idea on how I can go behind this? I did a bit of googling but I am not able to zero in exactly

推荐答案

原因

考虑这种情况。

这是错误的code线发生在try块返回前行。然后,它会去捕显示消息框,然后在最后再返回什么?????它返回的东西事业方法的返回类型为int。

An error occurs in code line before return line in try block. Then it will go in catch show messagebox and then in finally and then return what????? It has to return something cause method return type is int.

解决方案

做到这一点

    finally
    {
        GC.Collect();
    }
    return m_taskID;
}

这篇关于不是所有的code路径,而使用try,catch和finally返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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