Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 2
    isn't it risky to use the 'Try-Finally' (or the syntactical sugar - "using(){}") statement with unmanaged resources? Case in point, if the "Close" option fails, the exception is not caught, and finally may not run. Also, if there is an exception in the finally statement it can mask other exceptions. I think that is why Try-Catch is preferred. Commented Oct 30, 2013 at 13:54
  • Zack, not clear on your object; what am I missing? If the Close method throws an exception, the finally block will execute before the exception is thrown up. Right? Commented Jul 21, 2014 at 18:11
  • 1
    @jmoreno, I undid your edit. If you'll notice, there is no catch block at all in the method. The idea is that any exception that occurs (even in the finally) should be thrown, not silently caught. Commented Sep 12, 2014 at 15:48
  • 5
    @MattDavis Why do you need success flag at all? Why not try { Close(); } catch { Abort(); throw; }? Commented Dec 23, 2014 at 1:57
  • What about putting a try/catch around Close(); success = true;? I wouldn't want an exception thrown if I could successfully abort it in the finally block. I'd only want an exception thrown if the Abort() failed in that case. This way, the try/catch would hide the potential race condition exception and still allow you to abort() the connection in the finally block. Commented May 4, 2016 at 17:28