现在我的问题是:我完全看不出来,使异常被替换、包装、忽略或重新抛出是否有益的可配置性。根据我的经验,这个决定必须在编写代码时做出,因为当您更改异常处理行为时,通常必须更改周围或调用代码。The exception handling application block aims to centralize and make fully configurable with config files the following key exception handling tasks:
- Logging an Exception
- Replacing an Exception
- Wrapping an Exception
- Propagating an Exception
- etc.
The library does that by having you modify your try catch blocks as follows:
try { // Run code. } catch(DataAccessException ex) { bool rethrow = ExceptionPolicy.HandleException(ex, "Data Access Policy"); if (rethrow) { throw; } }
Based on what is specified in the app.config for the policy name (see here for docs), HandleException will either ...
- throw a completely new exception (replace the original exception)
- wrap the original exception in a new one and throw that
- swallow the exception (i.e. do nothing)
- have you rethrow the original exception
Additionally you can also configure it to do more stuff beforehand (e.g. log the exception).
例如,当您重新配置以便在特定点抛出的特定异常现在被忽略而不是重新抛出时,您的代码可能会开始表现不正确(在catch块之后可能会有不能执行的代码)。所有其他可能的异常处理更改也是如此(例如,替换->重新抛出,忽略->包装)。
因此,对我来说,底线是异常处理块解决了实际上不存在的问题。异常记录和通知部分是好的,但所有其他东西是否只是过度工程的完美例子?