如何在不同的返回类型函数中返回错误消息?

时间:2017-01-20 06:37:35

标签: c# error-handling

此函数连接到postgres数据库并返回数据集。

我想要理解的两件事

  1. 如果我收到错误,我该如何归还呢?
  2. 这是返回数据集的最佳方法吗?

    string strODBCDriverName = "DSN=Postgres_32";
    
    public DataSet SelectDataSet(string sql, bool isProcedure, Dictionary<string, object> parameters = null)    {
    using (OdbcConnection odbcConnection = new OdbcConnection(strODBCDriverName))
    {
        odbcConnection.Open();
        using (OdbcCommand odbcCommand = new OdbcCommand(sql, odbcConnection))
        {
    
            if (isProcedure) odbcCommand.CommandType = CommandType.StoredProcedure;
            else odbcCommand.CommandType = CommandType.Text;
    
            if (parameters != null)
                foreach (KeyValuePair<string, object> parameter in parameters)
                    odbcCommand.Parameters.AddWithValue(parameter.Key, parameter.Value);
    
            using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand))
            {
                using (DataSet ds = new DataSet())
                {
    
                    try
                    {
                        adapter.Fill(ds); return ds;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
    
                    }
                }
            }
        }
    }
    }
    

3 个答案:

答案 0 :(得分:0)

我认为如果你回归null会很棒;如果您需要返回一些自定义消息,则意味着您可以使用out参数。因此,如果在这种情况下发生任何异常,则返回值将为null out参数将保存异常详细信息。如果数据集填充良好意味着outParameter将具有值“Success”或类似的值。因此,方法签名将如下所示进行更改

public static DataSet SelectDataSet(string sql, bool isProcedure, out string message, Dictionary<string, object> parameters = null)
{
    // Rest of codes here

    try
    {
        message = "Success";
        adapter.Fill(ds);
        return ds;
    }
    catch (Exception ex)
    {
        message = ex.Message;
        return null;
    }

}

你可以这样调用这个方法:

string message = String.Empty;
DataSet resultDataset = SelectDataSet("query here", false, out message);
if (resultDataset != null)
{
    Console.WriteLine(message);
    // proceed with resultDataset
}
else
{
    Console.WriteLine(message);
}

如果有任何异常,resultDataset将为null,否则您可以继续使用其值。

答案 1 :(得分:0)

创建一个类:

class DataSetWithError: DataSet
{
    public Exception msg { get; set; }
}

在查询期间保存错误:

using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand))
{
    DataSetWithError ds = new DataSetWithError();

    try
    {
        adapter.Fill(ds);
    }
    catch (Exception ex)
    {
        ds.msg = ex;
    }
    finally
    {
        adapter.Close();
    }

    return ds;
}

结果:

DataSetWithError dataSetWithError = SelectDataSet();
if (dataSetWithError.msg == null)
{
    // Show data
}
else
{
    MessageBox.Show(dataSetWithError.msg.ToString());
}

答案 2 :(得分:0)

我喜欢可以重复使用的通用Result class

internal class Result
    {
    internal bool IsFailure => !IsSuccess;

    internal bool IsSuccess { get; }

    internal string Error { get; }

    protected Result(bool isSuccess, string error) {
        IsSuccess = isSuccess;
        Error = error;
    }

    private Result(bool isSuccess) : this(isSuccess, null) { }

    internal static Result Fail(string error) => new Result(false, error);

    internal static Result<T> Fail<T>(string error) =>
        new Result<T>(default(T), false, error);

    internal static Result Ok() => new Result(true);

    internal static Result<T> Ok<T>(T value) => new Result<T>(value, true);
}

internal sealed class Result<T> : Result
    {
    internal T Value { get; }

    internal Result(T value, bool isSuccess) : this(value, isSuccess, null) { }

    internal Result(T value, bool isSuccess, string error) : base(isSuccess, error) {
        Value = value;
    }

这不仅可以用于DataSet,还可以用于任何类型。

在你的情况下,返回将是Result<DataSet>并且返回可以变为:

returns ds - &gt; new Result.Ok(d)

throw ex - &gt; new Result.Fail<DataSet>(ex.Message)