【问题标题】:How to exit method from sub-method without throwing exception如何从子方法中退出方法而不抛出异常
【发布时间】:2022-01-20 10:13:03
【问题描述】:

我有以下代码,但我不确定退出Execute() 方法的正确模式是什么。

代码可以运行,但是Execute() 方法太大。

public void Execute()
{
    // load datatable from excel
    DataTable data = new();
    try
    {
        data = ExcelReader.ToDataTable(_meta.FullPath, new() { SheetName = _sheetName });
    }
    // these errors are fine, but the dataset is not relevant and we want exit the execution
    catch (ArgumentOutOfRangeException e)
    {
        if (e.Message.Contains("HeaderRowIndex")) return;
    }
    catch (ArgumentNullException e)
    {
        if (e.Message.Contains("Parameter 'sheet'")) return;
    }

    // execute logic on dataset
    // ..
}

我想将代码的load data 部分移动到像这样的单独方法中,但是我不知道如何结束执行。

public void Execute()
{
    // load data
    DataTable data = new();
    LoadData();

    // execute logic on dataset
    // ...
}

private DataTable LoadData()
{
    try
    {
        data = ExcelReader.ToDataTable(_meta.FullPath, new() { SheetName = _sheetName });
    }
    // these errors are fine, but the dataset is not relevant and we want exit the execution => but how?
    catch (ArgumentOutOfRangeException e)
    {
        if (e.Message.Contains("HeaderRowIndex")) return; // will return to Execute() method, but not exit it.
    }
    catch (ArgumentNullException e)
    {
        if (e.Message.Contains("Parameter 'sheet'")) return; // will return to Execute() method, but not exit it.
    }
}

我认为这是一个很常见的问题,那么处理此要求的推荐方法是什么?我是否应该从LoadData 方法创建一些返回对象,例如

class LoadDataResult
{
    public DataTable data {get; set};
    public bool IsRelevant {get; set};
}

dataResult = LoadData()
if (!dataResult.IsRelevant) return;

或者它是一种矫枉过正且更简单的解决方案(不使用这些结果对象填充程序)?

【问题讨论】:

  • DataTable data = LoadData(); .... do the stuff...?我不清楚问题是什么。当没有更多语句要运行或return 被命中时,执行将结束。
  • 为什么不简单地从 LoadData 返回null DataTable?
  • 如果您声明 private DataTable LoadData() 那么您应该返回一个 DataTable 或 null。然后很容易在Execute方法上检查返回值,如果为null则退出
  • private Tuple LoadData(){}如果要完成在元组的Item1和Item2中的数据
  • 没有这些结果对象,你可以再创建一个Execute方法,像套娃一样放在LoadData的末尾

标签: c# return


【解决方案1】:

我会考虑您要返回的内容。在您的捕获中,您可以记录错误,但您不需要返回。所有代码都将流回调用它的前一个方法,无论它是否返回。仅当您需要将值传递回前一个调用方法时才需要返回。否则,你就别管它了!

【讨论】:

  • 问题是他们希望在调用LoadData 后立即从Execute 方法返回,如果它记录错误并且基本上跳过Execute 中的逻辑。跨度>
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

您可以在输出参数中返回数据,并将数据加载的结果作为主方法输出返回(true表示加载成功,false表示错误即可):

 private bool TryLoadData(out DataTable data)
 {
    data = null;

    try
    {
        data = ExcelReader.ToDataTable(
           _meta.FullPath, new() { SheetName = _sheetName });
        return true; // loading succeeded
    }        
    catch (ArgumentOutOfRangeException e)
        when (e.Message.Contains("HeaderRowIndex"))
    {         
        // loading failed, continue to Execute method
        return false;
    }
    catch (ArgumentNullException e)
        when (e.Message.Contains("Parameter 'sheet'"))
    {
        // loading failed, continue to Execute method
        return false;
    }

    // otherwise exception propagates to Execute method
 }

用法:

public void Execute()
{
    if (TryLoadData(out var data)
    {
       // execute logic on dataset
    }
    else
    {
       // error, but not exception
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多