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. Go to finally in try block

Go to finally in try block

Scheduled Pinned Locked Moved C#
questiondatabaseworkspace
15 Posts 9 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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

    J L P J A 8 Replies Last reply
    0
    • V vanikanc

      Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

      J Offline
      J Offline
      Jibesh
      wrote on last edited by
      #2

      if you exit the try block it would automatically pass through finally block no matter how it exits the try block i.e either through catch or through return statement. have you tried to debug this. you may paste the code block if it still didnt work.

      Jibesh V P

      V 1 Reply Last reply
      0
      • J Jibesh

        if you exit the try block it would automatically pass through finally block no matter how it exits the try block i.e either through catch or through return statement. have you tried to debug this. you may paste the code block if it still didnt work.

        Jibesh V P

        V Offline
        V Offline
        vanikanc
        wrote on last edited by
        #3

        I am going to use a return. But this exits the whole program. So, within this condition, I am going to close the connection and have a return.

        J 1 Reply Last reply
        0
        • V vanikanc

          I am going to use a return. But this exits the whole program. So, within this condition, I am going to close the connection and have a return.

          J Offline
          J Offline
          Jibesh
          wrote on last edited by
          #4

          what makes the program to exit? is that how it has to work?

          Quote:

          So, within this condition, I am going to close the connection and have a return

          since you want to close the connection before the application exists thats the best place to do this reset.

          Jibesh V P

          V 1 Reply Last reply
          0
          • J Jibesh

            what makes the program to exit? is that how it has to work?

            Quote:

            So, within this condition, I am going to close the connection and have a return

            since you want to close the connection before the application exists thats the best place to do this reset.

            Jibesh V P

            V Offline
            V Offline
            vanikanc
            wrote on last edited by
            #5

            If the database returns 0 rows, exit else continue.

            J 1 Reply Last reply
            0
            • V vanikanc

              If the database returns 0 rows, exit else continue.

              J Offline
              J Offline
              Jibesh
              wrote on last edited by
              #6

              Ok. did I answer your question? if not can you please share the code for the better understanding of your problem.

              Jibesh V P

              D 1 Reply Last reply
              0
              • V vanikanc

                Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                vanikanc wrote:

                If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open.

                It will not end the program, but terminate it. Use Application.Terminate for a WinApp, and simply return from the Main method (the entry-point) if you're writing a Console App.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                1 Reply Last reply
                0
                • V vanikanc

                  Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  vanikanc wrote:

                  two try blocks, two catches and one finally block

                  You'd have to show the code for us to understand how you have them arranged.

                  vanikanc wrote:

                  closed the connection to database

                  You should be using a using statement as well -- it's similar to a try/finally.

                  vanikanc wrote:

                  Environment.Exit();

                  Don't do that; just return.

                  1 Reply Last reply
                  0
                  • V vanikanc

                    Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    vanikanc wrote:

                    then it will end the program, and I would not have closed the connection to database that is open.

                    When the program terminates the connection will be terminated as well. And the only way you can terminate a database connection in C# anyways is if all of the following are true. 1. You have configured the connection pool to keep no idle connections. 2. There are no connections in use 3. You reset the pool. Other than that just "closing" the connection does nothing more than return it to the connection pool.

                    1 Reply Last reply
                    0
                    • V vanikanc

                      Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                      A Offline
                      A Offline
                      Abhinav S
                      wrote on last edited by
                      #10

                      Just return from the method. The finally block will execute and then your method will return.

                      WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                      1 Reply Last reply
                      0
                      • V vanikanc

                        Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                        V Offline
                        V Offline
                        V 0
                        wrote on last edited by
                        #11

                        Some alternate ideas. - Are the two try/catch absolutely necessary? Try/catch is an expensive operation and I see many people using it where a simple if statement could save the day. - Perhaps you can remove the second try, but move the catch to the first try. you can "assign" multiple catch handlers to one try. Of course only valid if you specify the exceptions. eg.:

                        try{
                        //code here
                        }
                        catch(SqlExcpetion sqlex){
                        //handle sql exceptions
                        }
                        catch(IOException){
                        //handle io exceptions
                        }
                        catch(Exception ex){
                        //handle all other exceptions
                        }
                        finally{
                        //handle anything that needs to be handled like freeing file handles, closing connections, ...
                        //make sure you test for null values etc, because otherwise you'll get an error in the finally block, which is a bit silly.
                        }

                        - the quick and dirty way is maybe a goto statement, but you don't want to go that way, they're evil hope this helps.

                        V.
                        (MQOTD Rules and previous Solutions )

                        1 Reply Last reply
                        0
                        • V vanikanc

                          Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                          R Offline
                          R Offline
                          Rahul Rajat Singh
                          wrote on last edited by
                          #12

                          Just write return and the function will end after executing finally block.

                          Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.

                          1 Reply Last reply
                          0
                          • V vanikanc

                            Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?

                            R Offline
                            R Offline
                            Rahul Rajat Singh
                            wrote on last edited by
                            #13

                            One suggestion from my side. If you put you connection inside a using block, you will not have to rely on any specific piece of code to close the connection and you can simply return from anywhere. It is the recommended way of doing the connection business. This article will tell you how to use the connections with a using ASP.NET - How To Use(Open/Close) Connections Correctly[^]

                            Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.

                            D 1 Reply Last reply
                            0
                            • J Jibesh

                              Ok. did I answer your question? if not can you please share the code for the better understanding of your problem.

                              Jibesh V P

                              D Offline
                              D Offline
                              devvvy
                              wrote on last edited by
                              #14

                              Yes, I think the general concensus is that, put try-catch-finally in all thread functions as coding practice.

                              dev

                              1 Reply Last reply
                              0
                              • R Rahul Rajat Singh

                                One suggestion from my side. If you put you connection inside a using block, you will not have to rely on any specific piece of code to close the connection and you can simply return from anywhere. It is the recommended way of doing the connection business. This article will tell you how to use the connections with a using ASP.NET - How To Use(Open/Close) Connections Correctly[^]

                                Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.

                                D Offline
                                D Offline
                                devvvy
                                wrote on last edited by
                                #15

                                I know what happens to lock(x) in event of a Thread.Abort, what about "using"?

                                dev

                                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