返回值的方法无法传递给异常处理程序

时间:2014-06-19 19:25:16

标签: c# .net winforms exception-handling mvp

我在方法(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; };
}

3 个答案:

答案 0 :(得分:2)

您正在

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

操作不返回值。他们总是“回归无效”可以这么说。

您需要更改为:

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
  }
}

答案 1 :(得分:2)

使用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);
}

答案 2 :(得分:2)

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

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; };
}
相关问题