C# using statement catch error

后端 未结 16 1594
轻奢々
轻奢々 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:40

    one issue with "using" is that it doesn't handles exceptions. if the designers of "using" would add "catch" optionally to its syntax like below pseudocode, it would be much more useful:

    using (...MyDisposableObj...)
    {
    
       ... use MyDisposableObj ...
    
    catch (exception)
    
       ... handle exception ...
    
    }
    
    it could even have an optional "finally" clause to cleanup anything other than the "MyDisposableObj" allocated at the beginning of the "using" statement... like:
    
    using (...MyDisposableObj...)
    {
    
       ... use MyDisposableObj ...
       ... open a file or db connection ...
    
    catch (exception)
    
       ... handle exception ...
    
    finally
    
       ... close the file or db connection ...
    
    }
    

    still there'll be no need to write code to dispose of MyDisposableObj b/c it'd be handled by using...

    How do like that?

提交回复
热议问题