为什么不是所有的代码路径都返回一个值 [英] why getting not all code paths return a value

查看:56
本文介绍了为什么不是所有的代码路径都返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我会得到不是所有的代码路径都为下面的代码返回值以及如何解决?

Why am I getting not all code paths return a value for the below code and how to fix it?

private DateTime get_business_date()
{
    try
    {
        sql_connection = new SqlConnection(my_connection);
        sql_connection.Open();

        sql_command = new SqlCommand("sp_get_business_date", sql_connection);
        sql_command.CommandType = CommandType.StoredProcedure;
        sql_reader = sql_command.ExecuteReader();

        if (sql_reader.Read())
        {
            if (sql_reader.HasRows)
            {
                business_date = Convert.ToDateTime(sql_reader["category_name"];
            }
        }

        return business_date;
    }
    catch (Exception exp)
    {
        XtraMessageBox.Show(String.Format("Error reading business date!!{0}{1}", Environment.NewLine, exp.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (sql_reader != null) { sql_reader.Close(); sql_reader.Dispose(); }
        if (sql_connection != null) { if (sql_connection.State == ConnectionState.Open) { sql_connection.Close(); sql_connection.Dispose(); } }
    }
}

谢谢

Jassim

推荐答案

解决方案之一是添加'return DateTime.MinValue;'在消息框后.此值将指示错误,因此, get_business_date 必须使用'if'分析结果.

One of the solutions is to add ‘return DateTime.MinValue;’ after the message box. This value will be the indicator of the error, therefore the callers of get_business_date will have to analyse the result using an ‘if’.

另一种解决方案:在消息框后添加"throw;" .


这篇关于为什么不是所有的代码路径都返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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