不能将返回值的方法传递给异常处理程序

本文关键字:异常处理程序 方法 返回值 不能 | 更新日期: 2023-09-27 18:09:19

我在一个方法(ExecuteAction(Action action))中集中处理异常。我有一个问题,当传递动作到这个方法时,它返回一个值。

在以下代码中,我得到这个错误:

Since 'System.Action' returns void, a return keyword must not be followed by an object expression

如何解决这个问题?

public decimal CalculateInstalment(decimal amount, int months)
{
    this.ExecutAction(() =>
    {
        var result = amount / months;
        return Math.Round(result, 2);
    });
}
protected bool ExecutAction(Action action)
{
    try
    {
        action();
        return true;
    }
    catch (NullReferenceException e) { _MessageService.ShowErrorMessage(e); return false; ; }
    catch (System.Data.SqlTypes.SqlTypeException e) { _MessageService.ShowErrorMessage(e); return false; }
    catch (System.Data.SqlClient.SqlException e) { _MessageService.ShowErrorMessage(e); return false; }
    catch (System.Exception e) { _MessageService.ShowErrorMessage(e); return false; };
}

不能将返回值的方法传递给异常处理程序

正如其他人所说,操作类型不返回值,但您可以引用外部变量。如果您试图获取该值,请考虑这样的设置:

public decimal CalculateInstalment(decimal amount, int months)
{
    var result = 0.0;
    this.ExecutAction(() =>
    {
        result = Math.Round((amount / months), 2);
    });
    return result;
}
protected bool ExecutAction(Action action)
{
    try
    {
        action();
        return true;
    }
    catch (NullReferenceException e) { _MessageService.ShowErrorMessage(e); return false; ; }
    catch (System.Data.SqlTypes.SqlTypeException e) { _MessageService.ShowErrorMessage(e); return false; }
    catch (System.Data.SqlClient.SqlException e) { _MessageService.ShowErrorMessage(e); return false; }
    catch (System.Exception e) { _MessageService.ShowErrorMessage(e); return false; };
}

使用bool ExecutAction(Action action)执行返回值的委托没有多大意义-您如何期望从该委托检索值?

除了ExecuteAction方法之外,还应该为返回值的委托使用TryXXX模式(如BCL中的各种TryParse方法所示):

protected T TryExecutAction<T>(Func<T> func, out bool success)
{
    try
    {
        T temp = func();
        success = true;
        return temp;
    }
    catch (NullReferenceException e) { _MessageService.ShowErrorMessage(e);  }
    catch (System.Data.SqlTypes.SqlTypeException e) { _MessageService.ShowErrorMessage(e); }
    catch (System.Data.SqlClient.SqlException e) { _MessageService.ShowErrorMessage(e);  }
    catch (System.Exception e) { _MessageService.ShowErrorMessage(e); };
    success = false;
    return default(T);
}

不要忘记将值返回给调用者:

public decimal CalculateInstalment(decimal amount, int months)
{
    bool success;
    return this.TryExecutAction(() =>
    {
        var result = amount / months;
        return Math.Round(result, 2);
    }, out success);
}

您正在返回

中的值
this.ExecutAction(() =>
{
    var result = amount / months;
    return Math.Round(result, 2);
    });
}

操作不返回值。可以说,它们总是"返回void"。

您需要更改为:

protected bool ExecutAction(Func<object> fn)
顺便说一句,这对我来说真的很"臭"…我想这是样品吧?
protected T Execute<T>(Func<T> fn) {
  try {
    return fn();
  }
  catch (Exception ex) {
    // do whatever
    // return null and check for it. null checking uuuuuggghhhhh
  }
}