The fundamental limitation of .net exception handling is that since it focuses on the C ++ exception handling model, all information related to the current exception context must be encapsulated in a single exception object; indeed, in C # all information related to whether an exception should be thrown should be encapsulated in a single type exception object. In addition, in C #, the only way to find out about an exception is to agree to its trick, and there is no way to declare declaratively that someone wants to act against the exception, but has no intention of handling it enough to be considered "allowed." The indicated difficulties with “use” arise from these limitations.
In the real world, things may happen during Dispose() , which should disrupt the flow of any code that does not expect them. Such cases should usually be exceptions. Unfortunately, if Dispose triggered because some other exception occurred, and an exception occurs in it, there are only three practical courses in C #:
- There is no way to lose all information about the exception that caused Dispose to run, because there is no way for Dispose or the code that calls it to receive this information, and any exception that occurs in the finally block will destroy any information about any previously pending instructions.
- Eliminate the exception that occurred in `Dispose` - you could register it somewhere, but if the caller fuzzily checks the log, he will not know that the exception has occurred.
- Catch any exception of all types, capture its type of variable and re-challenge; Act on the variable in question in the finally block.
There is a fourth possibility in vb.net that has better semantics, but requires icky-looking code: Exception and use exception filters to catch, without catching, any exception that occurs with a variable and, like C # 3 above, act on this a variable in the finally block.
The using statement offers the first semantics above. In some situations, other approaches may be better. I want vb and C # to suggest a version of "finally" that Exception type parameter. This may allow the use of semantics # 4 above (which is semantically the most beautiful), available without ugly code.
source share