C# using statement catch error

后端 未结 16 1333
轻奢々
轻奢々 2021-01-30 14:06

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code:

 using (SqlComma         


        
16条回答
  •  旧巷少年郎
    2021-01-30 14:53

    If the caller of your function is responsible for dealing with any exceptions the using statement is a nice way of ensuring resources are cleaned up no matter the outcome.

    It allows you to place exception handling code at layer/assembly boundaries and helps prevent other functions becoming too cluttered.

    Of course, it really depends on the types of exceptions thrown by your code. Sometimes you should use try-catch-finally rather than a using statement. My habit is to always start with a using statement for IDisposables (or have classes that contain IDisposables also implement the interface) and add try-catch-finally as needed.

提交回复
热议问题