C# 文件异常干燥原理C

C# 文件异常干燥原理C,c#,error-handling,try-catch,dry,C#,Error Handling,Try Catch,Dry,在C中执行许多不同的文件处理时,始终使用try-catch块,如下所示。有没有一种方法可以将其封装在泛型类中,这样我就不必重复了 我只想在一个足够灵活的类中尝试catch和handle,这样我就可以向其中添加处理程序了 // The caller does not have the required permission. Catch(UnauthorizedAccessException uae) { } // sourceFileName or destFileName is a zer

在C中执行许多不同的文件处理时,始终使用try-catch块,如下所示。有没有一种方法可以将其封装在泛型类中,这样我就不必重复了

我只想在一个足够灵活的类中尝试catch和handle,这样我就可以向其中添加处理程序了

// 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)
{

}

你有很多选择。仅提及其中两项:

选项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:使用异常过滤器 此选项实际上将捕获每个异常,除非您还使用过滤器C6+

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
}

创建一个具有可重写方法的基类,并将其放入try-catch中,然后捕获上述所有异常。类可以继承它并重写基方法,异常处理将在基中完成,因为它将冒泡。顺便说一句,您可能需要交换ArgumentException和ArgumentNullException。ArgumentException是ArgumentNullException的基类,应该拦截所有参数异常。在第一个示例中,我将在异常包装器中包装函数,对吗?是的,注释//do your thing位于lambda函数中,该函数在下一个函数中作为操作执行。