Catching Exception inside Using statement

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

    Just in the normal way:

    Either

    try
    {
        using(SqlConnection conn = new SqlConnection(connString)) 
        { 
          //some code        
        } 
    }
    catch (Exception exc)
    {
        //handle error
    }
    

    or

    using(SqlConnection conn = new SqlConnection(connString)) 
    { 
        try
        {
            //some code 
        }
        catch (Exception exc)
        {
            //handle error
        }                
    } 
    
    0 讨论(0)
  • 2021-02-04 02:03

    You don't do it inside the using

    try
    {
        using(SqlConnection conn = new SqlConnection(connString))
        {
            // Some code for when "conn" is succesfully instantiated
        }
    }
    catch (SomeSpecificConnectionInstantiationException ex)
    {
        // Use ex to handle a bizarre instantiation exception?
    }       
    
    0 讨论(0)
  • 2021-02-04 02:04

    It is a good practice to use the try{}catch(){} inside the using statement if you want to catch an exception thrown by the code inside the using block. Now, consider the following two examples - that explains why try-catch block inside the using statement is a good practice.

    Example 1

           try{
               using(SomeObject so = new SomeObject){
                  // Perform some tasks
               }
           }catch(SomeException objSomeException){
                 // Perform some actions, if exception occurs
           }
    

    Example 2

           using(SomeObject so = new SomeObject){
               try{    
                   // Perform some tasks
                  }catch(SomeException objSomeException){
                       // Perform some actions, if exception occurs
                  }
           }
    

    Now, if an exception occurs while performing some tasks inside the using statement, will both example have the same results. The simple answer is no, reason???

    When an exception occurs in the example 1, it is caught by the catch block - without reaching the end of the using block. Therefore, the someObject in example 1 will not get disposed properly. Even if CLR is generous (which you should not count on) - the memory used by the someObject in the example 1 will not be recovered (or maximum it will end up in Generation 2 GC collection).

    Where in case of the example 2, the catch block is inside the using statement. That means that the execution will reach the end of the using block. Therefore, your object will be disposed and you won't have to worry about the memory leakage (spoilage,

    0 讨论(0)
  • 2021-02-04 02:15
    class SqlConnection
    {
       using(sqlConnection)
       {
    
       }
    }
    
    class Consumer
    {
       try
      {
    
      }
      catch(SqlException)
      {
    
      }
    
    }
    

    It is up to the consumer of the class to decide what to do with the exception.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 02:22

    Thats just the same way you would do it without.

    using(SqlConnection conn = new SqlConnection(connString))
    {
      try{
        //some code
      }
      catch(SqlException e)
        MessageBox.Show(e.Message);
    }
    
    0 讨论(0)
提交回复
热议问题