不是所有的code路径,而使用try,catch和finally返回一个值有的、路径、不是、catch

由网友(月光下等你)分享简介:当我执行下面的函数,我得到一个错误:When I execute the following function, I get an error:错误Excel1.WebServiceFunctions.CreateMyTask():不是所有的code路径返回一个值"Error 'Excel1.WebServiceF...

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

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;
}
阅读全文

相关推荐

最新文章