繁体   English   中英

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

[英]A Method which return a value cannot be passed to exception handler

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

您正在返回一个值

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM