在图书馆异常处理政策 [英] Exception handling policy in libraries

查看:162
本文介绍了在图书馆异常处理政策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建.NET库,什么是你的异常处理政策?在具体的,什么是你对处理内部库调用异常并将它们暴露在调用代码政策?



例如,




  • 您会给予的库函数的任何其他,从而让所有的异常不能处理流出它就好了?

  • 你创造该库自定义异常?

  • 您会捕获所有异常并抛出图书馆的例外呢?你设置原始异常作为库的异常内部异常?

  • 将如何在DB库的依赖会影响您的异常处理政策?



你会建议对异常处理在.NET库?


解决方案

您会给予的库函数的任何其他,从而让所有的
例外它不能处理流出来$ b $乙它作为-is?




是的,这肯定是默认的策略。




您是否创建该库自定义异常?




是的,如果呼叫者可以想见,做一些关于形势这样做,他们需要能够例外其他异常区分开来。但是,这是相当罕见的。




将如何在DB库的依赖会影响您的异常处理政策?




一个数据库的依赖可能带来曝光,让主叫方指定库如何处理某些异常的设置(例如, MaximumDeadlockRetries




你能捕获所有异常并抛出图书馆的例外呢?
你设置原始异常
作为图书馆的异常内部
例外呢?




没有,不是所有的异常。对于具体的例外,它的远程可能的,虽然当我的图书馆已经尝试来处理异常(如上面的数据库场景)和失败的我能想到的,我可能要做到这一点的唯一情况。


When building a .NET library, what's your exception handling policy? In specific, what's your policy about handling exceptions inside library calls and exposing them to calling code?

For example,

  • Would you treat a library function as any other, thus letting all exceptions it can't handle flow out of it as-is?
  • Would you create a custom exception for that library?
  • Would you catch all exceptions and throw the library's exception instead? Would you set the original exception as the library's exception internal exception?
  • How would the library dependence on a DB affect your exception-handling policy?

What guidelines and rules would you suggest for exception-handling in a .NET library?

解决方案

Would you treat a library function as any other, thus letting all exceptions it can't handle flow out of it as-is?

Yes, this is definitely the default strategy.

Would you create a custom exception for that library?

Yes, if callers can conceivably do something about the situation and to do so they need to be able to distinguish the exception from other exceptions. But that's pretty rare.

How would the library dependence on a DB affect your exception-handling policy?

A database dependency might entail exposing settings that let callers specify how the library handles certain exceptions (e.g., MaximumDeadlockRetries).

Would you catch all exceptions and throw the library's exception instead? Would you set the original exception as the library's exception internal exception?

No, not all exceptions. For specific exceptions, it's remotely possible, though the only case I can think of where I might want to do this is when my library has already tried to handle the exception (as in the database scenario above) and failed.

这篇关于在图书馆异常处理政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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