Catching Exception inside Using statement

后端 未结 9 698
不知归路
不知归路 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:18

    As other answers have stated, just add a normal try/catch.

    However, I would add that this is the wrong place to put that try/catch, especially if your goal is "show the users" a message. Let the exception happen at this level, and allow it to bubble up the stack to code that's in a better position to know how to respond to it.

    In other words, leave your code sample as it is. Don't add anything new... to that method. But perhaps the code that calls this method should be thinking about how to handle an exception ... any exception... from the database.

提交回复
热议问题