gpt4 book ai didi

c# - 库中的异常处理策略

转载 作者:太空狗 更新时间:2023-10-29 23:35:12 24 4
gpt4 key购买 nike

构建 .NET 库时,您的异常处理策略是什么?具体来说,关于在库调用中处理异常并将它们暴露给调用代码,您的政策是什么?

例如,

  • 您是否会像对待其他任何库函数一样对待库函数,从而让它无法处理的所有异常按原样流出?
  • 您会为该库创建自定义异常(exception)吗?
  • 您会捕获所有异常并抛出库的异常吗?您是否会将原始异常设置为库的异常内部异常?
  • 图书馆对数据库的依赖会如何影响您的异常处理政策?

对于 .NET 库中的异常处理,您会建议哪些准则和规则?

最佳答案

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?

是的,如果调用者可以想像地对这种情况采取一些措施并且这样做,他们需要能够将异常与其他异常区分开来。但这种情况非常罕见。

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

数据库依赖项可能需要公开设置,让调用者指定库如何处理某些异常(例如,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?

不,不是所有的异常(exception)。对于特定的异常,它是远程可能的,尽管我能想到的唯一情况是我的库已经尝试处理异常(如上面的数据库场景)但失败了。

关于c# - 库中的异常处理策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2765086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com