【问题标题】:File Exceptions DRY principle C#文件异常 DRY 原理 C#
【发布时间】:2019-03-03 20:42:34
【问题描述】:

在 C# 中执行许多不同的文件处理时,请始终使用如下图所示的 try catch 块。有没有办法将它封装在一个通用类中,所以我不需要重复自己 DRY 。

我想简单地尝试 catch 然后在一个足够灵活的类中处理,我可以向它添加处理程序..

// The caller does not have the required permission.
Catch(UnauthorizedAccessException uae)
{

}

// sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
// -or- sourceFileName or destFileName specifies a directory.
Catch(ArgumentException ae)
{

}

// sourceFileName or destFileName is null.
Catch(ArgumentNullException ane)
{

}

// The specified path, file name, or both exceed the system-defined maximum length.
Catch(PathTooLongException ptle)
{

}

// The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).
Catch(DirectoryNotFoundException dnfe)
{

}

// sourceFileName was not found.
Catch(FileNotFoundException fnfe
{

}

// destFileName exists. -or- An I/O error has occurred.
Catch(IOException ioe)
{

}

// sourceFileName or destFileName is in an invalid format.
Catch(NotSupportedException nse)
{

}

【问题讨论】:

  • 创建一个具有可覆盖方法的基类,并将其放入 try catch 并捕获所有上述异常。类可以继承它并覆盖基方法,异常处理将在基中完成,因为它会冒泡。
  • 顺便说一句,您可能想要交换 ArgumentExceptionArgumentNullExceptionArgumentExceptionArgumentNullException 的基类,应该拦截所有参数异常。

标签: c# error-handling try-catch dry


【解决方案1】:

这里有很多选择。仅提及其中 2 个:

选项 1:包装器和操作。

public void ProcessFile()
{
    ExceptionFilters.CatchFileExceptions( () => {
        // .. do your thing
    });
}

// somewhere else
public static class ExceptionFilters
{
    public static void CatchFileExceptions(Action action)
    {
        try
        {
            action();
        }
        catch(ExceptionTypeA aex)
        {
        }
        // ... and so on
        catch(Exception ex)
        {
        }
    }
}

选项 2:使用异常过滤器 此选项实际上会捕获所有异常,除非您还使用过滤器 (C# 6+)

public void ProcessFile()
{
    try
    {
        // do your thing
    }
    catch(Exception ex)
    {
        if(!ProcessFileExceptions(ex))
        {
            throw; // if above hasn't handled exception rethrow
        }
    }
}

public static void ProcessFileExceptions(Exception ex)
{
    if(ex is ArgumentNullException)
    {
        throw new MyException("message", ex); // convert exception if needed
    }

    // and so on

    return true;
}

您还可以在此处过滤您感兴趣的异常:

public void ProcessFile()
{
    try
    {
        // do your thing
    }
    catch(Exception ex) when(IsFileException(ex))
    {
        if(!ProcessFileExceptions(ex))
        {
            throw; // if above hasn't converted exception rethrow
        }
    }
}

public static bool IsFileException(Exception ex)
{
    return ex is ArgumentNullException || ex is FileNotFoundException; // .. etc
}

【讨论】:

  • 在第一个示例中 - 我会将函数包装在异常包装器中,对吗?
  • 是的,注释 // do your thing 在 lambda 函数中,在下一个函数中作为 action() 执行。
猜你喜欢
  • 2022-10-23
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多