none
this.Close() within try - catch RRS feed

  • Question

  • Hi,

    I have a try - catch and I want to ask, if I want to close the form after the successful run, should I keep the this.Close() in the try or this will cause the finally not to perform therefore I will have my sql connections open (because I am closing all connections in finally?

    Thanks,

    Jassim

    Thursday, January 21, 2016 10:27 AM

Answers

  • so then where can I close the form using this.close()? and if I put it in the try it will skip the (finally) where  am cleaning my sql connection so will this have any impact on the performance?

    @Jassim,

    (this.close())When a form is closed, all resources created within the object are closed and the form is disposed.

    Dispose disposes the form resources altogether, and you won't be able to re-use the form instance again.

    Here is how to use clean sql connection

    SqlConnection conn;
    try
    {
        //`using` scope operations are executed here
        conn = new SqlConnection(_dbconnstr));
    
    }
    catch
    {
        //exceptions are bubbled
        throw;
    }
    finally
    {
        //Dispose is always called
        conn.Dispose();
    }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 22, 2016 3:13 AM

All replies

  • According to documentation, if errors do not happen, then the program executes the body of try, then skips all of the catch blocks and executes finally. In case of exception, it executes only a part of the body (until exception), then the corresponding catch, and finally it executes the finally block.

    Therefore finally is executed in any case, even if exception is thrown or there is a return statement within try or catch.

    • Proposed as answer by Ashish Pandey Thursday, January 21, 2016 12:32 PM
    Thursday, January 21, 2016 12:09 PM
  • Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw.

    • try: A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

    • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

    • finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

    • throw: A program throws an exception when a problem shows up. This is done using a throw keyword.


    Ashish Pandey

    Thursday, January 21, 2016 12:36 PM
  • so then where can I close the form using this.close()? and if I put it in the try it will skip the (finally) where  am cleaning my sql connection so will this have any impact on the performance?
    Thursday, January 21, 2016 1:00 PM
  • so then where can I close the form using this.close()? and if I put it in the try it will skip the (finally) where  am cleaning my sql connection so will this have any impact on the performance?

    @Jassim,

    (this.close())When a form is closed, all resources created within the object are closed and the form is disposed.

    Dispose disposes the form resources altogether, and you won't be able to re-use the form instance again.

    Here is how to use clean sql connection

    SqlConnection conn;
    try
    {
        //`using` scope operations are executed here
        conn = new SqlConnection(_dbconnstr));
    
    }
    catch
    {
        //exceptions are bubbled
        throw;
    }
    finally
    {
        //Dispose is always called
        conn.Dispose();
    }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 22, 2016 3:13 AM