C#:抛出自定义异常最佳实践 [英] C#: Throwing Custom Exception Best Practices

查看:36
本文介绍了C#:抛出自定义异常最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于 C# 异常处理实践的其他问题,但似乎没有人问我在寻找什么.

I have read a few of the other questions regarding C# Exception Handling Practices but none seem to ask what I am looking for.

如果我为特定类或类集实现自己的自定义异常.与这些类相关的所有错误是否应该使用内部异常封装到我的异常中,还是应该让它们通过?

If I implement my own custom Exception for a particular class or set of classes. Should all errors that relate to those classes be encapsulated into my exception using inner exception or should I let them fall through?

我认为最好捕获所有异常,以便可以立即从我的源中识别异常.我仍然将原始异常作为内部异常传递.另一方面,我认为重新抛出异常是多余的.

I was thinking it would be better to catch all exceptions so that the exception can be immediately recognized from my source. I am still passing the original exception as an inner exception. On the other hand, I was thinking it would be redundant to rethrow the exception.

例外:

class FooException : Exception
{
    //...
}

选项 1:Foo 封装所有异常:

class Foo
{
    DoSomething(int param)
    {
        try 
        {
             if (/*Something Bad*/)
             {  
                 //violates business logic etc... 
                 throw new FooException("Reason...");
             }
             //... 
             //something that might throw an exception
        }
        catch (FooException ex)
        {
             throw;
        }
        catch (Exception ex)
        {
             throw new FooException("Inner Exception", ex);
        }
    }
}

选项 2:Foo 抛出特定的 FooExceptions 但允许其他异常失败:

class Foo
{
    DoSomething(int param)
    {
        if  (/*Something Bad*/)
        {
             //violates business logic etc... 
             throw new FooException("Reason...");
        }
        //... 
        //something that might throw an exception and not caught
    }
}

推荐答案

根据我在库方面的经验,出于以下几个原因,您应该将所有内容(您可以预期的)包装在 FooException 中:

Based on my experience with libraries, you should wrap everything (that you can anticipate) in a FooException for a few reasons:

  1. 人们知道它来自您的课程,或者至少是他们对课程的使用.如果他们看到 FileNotFoundException,他们可能会四处寻找.你在帮助他们缩小范围.(我现在意识到堆栈跟踪用于此目的,所以也许您可以忽略这一点.)

  1. People know it came from your classes, or at least, their usage of them. If they see FileNotFoundException they may be looking all over for it. You're helping them narrow it down. (I realize now that the stack trace serves this purpose, so maybe you can ignore this point.)

您可以提供更多上下文.用你自己的异常包装一个 FNF,你可以说我试图加载这个文件为此目的,但找不到它.这暗示了可能的正确解决方案.

You can provide more context. Wrapping an FNF with your own exception, you can say "I was trying to load this file for this purpose, and couldn't find it. This hints at possible correct solutions.

您的库可以正确处理清理.如果您让异常冒泡,您就是在强迫用户进行清理.如果你正确地封装了你正在做的事情,那么他们不知道如何处理这种情况!

Your library can handle cleanup correctly. If you let the exception bubble, you're forcing the user to clean up. If you've correctly encapsulated what you were doing, then they have no clue how to handle the situation!

记住只包装您可以预期的异常,例如 FileNotFound.不要只是包装 Exception 并希望最好.

Remember to only wrap the exceptions you can anticipate, like FileNotFound. Don't just wrap Exception and hope for the best.

这篇关于C#:抛出自定义异常最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆