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

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

问题描述

我看过一些关于C#异常处理做法的其他问题,但没有似乎要问什么我期待的。

如果我实现我自己的自定义异常特定类或一组类。应该说涉及到这些类的所有错误使用内部异常或者我应该让他们倒下通过封装到我的例外呢?

我想这将是更好地捕获所有异常,这样异常可以从我的源立即确认。我仍然通过原始异常作为内部异常。在另一方面,我想这将是多余的重新抛出异常。

例外:

 类FooException:异常
{
    // ...
}

选项1:富encasulates所有异常:

 类Foo
{
    DoSomething的(INT参数)
    {
        尝试
        {
             如果(/ *坏事* /)
             {
                 //违反业务逻辑等..
                 抛出新FooException(理......);
             }
             // ...
             //一些可能引发异常
        }
        赶上(FooException前)
        {
             扔;
        }
        赶上(异常前)
        {
             抛出新FooException(内部异常,前);
        }
    }
}

选项2:富抛出具体FooExceptions但允许其他异常落空:

 类Foo
{
    DoSomething的(INT参数)
    {
        如果(/ *坏事* /)
        {
             //违反业务逻辑等..
             抛出新FooException(理......);
        }
        // ...
        //东西,可能会抛出异常,而不是抓
    }
}


解决方案

根据我与图书馆的经验,你应该换行的一切(你可以预见)一个 FooException 有几个原因:


  1. 人们知道它从你的班,或者至少,他们其中的用法来了。如果他们看到 FileNotFoundException异常他们可能会到处找它。你帮助他们缩小它。 (我现在的堆栈跟踪服务这一目的实现了,所以也许你可以忽略这一点。)


  2. 您可以提供更多的背景。包装的FNF与自己的异常,你可以说:我试图加载此文件的为此的,并不能找到它。这暗示了可能的解决方案的正确


  3. 您库可以正确处理清理。如果让异常泡沫,你强迫用户进行清理。如果你正确地封装你在做什么,他们不知道如何处理这种情况!


请记住,只有你包可以预见的异常,如 FileNotFound 。不要只是包装例外和最好的希望。

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.

Exception:

class FooException : Exception
{
    //...
}

Option 1: Foo encasulates all Exceptions:

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

Option 2: Foo throws specific FooExceptions but allows other Exceptions to fall through:

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

解决方案

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

  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.)

  2. 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.

  3. 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!

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天全站免登陆