Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Will the connection close?

Will the connection close?

Scheduled Pinned Locked Moved C#
questioncsharp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rafferty Uy
    wrote on last edited by
    #1

    Hi! Simple C# question, I'm just wondering the the connection will close in the code below:

    try {
    connection.Open();
    command.ExecuteNonQuery();
    throw new Exception();
    } catch(Exception) {
    throw;
    }
    finally {
    connection.Close();
    }

    Rafferty

    R N 2 Replies Last reply
    0
    • R Rafferty Uy

      Hi! Simple C# question, I'm just wondering the the connection will close in the code below:

      try {
      connection.Open();
      command.ExecuteNonQuery();
      throw new Exception();
      } catch(Exception) {
      throw;
      }
      finally {
      connection.Close();
      }

      Rafferty

      R Offline
      R Offline
      rah_sin
      wrote on last edited by
      #2

      yes becouse finally block gets excecuted in any situation whether there is exception or code excecutes successfully.

      rahul

      R 1 Reply Last reply
      0
      • R Rafferty Uy

        Hi! Simple C# question, I'm just wondering the the connection will close in the code below:

        try {
        connection.Open();
        command.ExecuteNonQuery();
        throw new Exception();
        } catch(Exception) {
        throw;
        }
        finally {
        connection.Close();
        }

        Rafferty

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Yes

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • R rah_sin

          yes becouse finally block gets excecuted in any situation whether there is exception or code excecutes successfully.

          rahul

          R Offline
          R Offline
          Rafferty Uy
          wrote on last edited by
          #4

          So, in the statement sequence, will the connection.Close() be executed before or after the exception is rethrown in the catch(...) block? If after, what if this whole thing is contained in another try... catch statement, will the catch statement in the parent next be executed first?

          Rafferty

          N R 2 Replies Last reply
          0
          • R Rafferty Uy

            So, in the statement sequence, will the connection.Close() be executed before or after the exception is rethrown in the catch(...) block? If after, what if this whole thing is contained in another try... catch statement, will the catch statement in the parent next be executed first?

            Rafferty

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            What happened when you tried? All you are asking can be easily checked by writing a simple program.

            Navaneeth How to use google | Ask smart questions

            R 1 Reply Last reply
            0
            • N N a v a n e e t h

              What happened when you tried? All you are asking can be easily checked by writing a simple program.

              Navaneeth How to use google | Ask smart questions

              R Offline
              R Offline
              Rafferty Uy
              wrote on last edited by
              #6

              haha okay okay, i'll try then.

              Rafferty

              1 Reply Last reply
              0
              • R Rafferty Uy

                So, in the statement sequence, will the connection.Close() be executed before or after the exception is rethrown in the catch(...) block? If after, what if this whole thing is contained in another try... catch statement, will the catch statement in the parent next be executed first?

                Rafferty

                R Offline
                R Offline
                rah_sin
                wrote on last edited by
                #7

                it will excecuted after the exception is rethrown and the outer catch block will be excecuted next.

                rahul

                R 1 Reply Last reply
                0
                • R rah_sin

                  it will excecuted after the exception is rethrown and the outer catch block will be excecuted next.

                  rahul

                  R Offline
                  R Offline
                  Rafferty Uy
                  wrote on last edited by
                  #8

                  You're right! I just made a small experiment as Navaneeth suggested and it looks like the case. Thanks much!

                  Rafferty

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups