Catching Exception inside Using statement

后端 未结 9 668
不知归路
不知归路 2021-02-04 01:27

I know that Using statement disposes out the object that is being created. Like if I wanted to do something like this:

    Using(SqlConnection conn = new SqlConn         


        
9条回答
  •  囚心锁ツ
    2021-02-04 02:22

    When you embed the using() block inside a try/catch, the using() block will indeed guarantee that Dispose is called. However, if non-managed code anywhere within your using block throws an exception, using() will just eat it and it won't reach your catch. Use try/catch inside the using() block, skip using() and do a try/catch/finally, or use the odd "using() try" syntax with a catch block (which leaves you with an odd number of brackets and is likely to confuse the heck out of mid-level programmers who later encounter it.

提交回复
热议问题