Do "instructions" use "bad code"?

I read that is used like this:

using (myObject) { myObject.DoStuff(); } 

You might think so:

 try { myObject.DoStuff(); } finally { myobject.Dispose() } 

So, if myObejct.DoStuff throws an ExceptionA and then myObject.Dispose () also throws an exception (ExceptionB), then ExceptionA will be lost. (See MSDN examples here for a better description.)

Does this mean that if the code inside the used block code can throw an exception (what is the most code on the right?), Then using is bad practice?

+4
source share
4 answers

Does this mean that if the code inside the used block code can throw an exception (what suits the code best?), Then using the instruction is bad practice?

Not.

and then myObject.Dispose () also throws an exception

This is really the essence of your question.

This is "bad practice" here. IDisposable.Dispose implementations must really be designed so that they do not throw exceptions, except in situations that are really fatal.

Since IDisposable really intended for the release of this resource, the main problem should be that this implementation does not throw in most cases. Calling the cleanup method will lead to a big chagrin - and for this reason the operator should not be used with the WCF client , etc.

That being said, I do not think that using the statement itself is bad practice. In fact, it tends to be very good practice, as it avoids the very common trap (without the possibility of using the resource in the event of an exception).

+18
source
Operators

using ensure that types that implement IDisposable are removed correctly (i.e., syntactic sugar for the correct implementation of the Dispose pattern ).

This is a very good good practice .

Having a reset of the Dispose function is bad practice.

+6
source

This is a common problem in WCF, where Dispose () often throws exceptions. There is a way to use a one-time wrap so you can continue to take advantage of the using () statement without risking losing an exception. It essentially swallows any exception thrown during Dispose, so the original exception is always one that falls into a higher context.

http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html

+6
source

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.

+2
source

Source: https://habr.com/ru/post/1403523/


All Articles