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. Exception in finally black ???

Exception in finally black ???

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 8 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.
  • M Offline
    M Offline
    md_azy
    wrote on last edited by
    #1

    Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

    M M A N C 5 Replies Last reply
    0
    • M md_azy

      Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Should be the same as any other exception that is not handled, i.e. it breaks! why don't you try it?

      Life goes very fast. Tomorrow, today is already yesterday.

      1 Reply Last reply
      0
      • M md_azy

        Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

        M Offline
        M Offline
        Manas Bhardwaj
        wrote on last edited by
        #3

        You can write a piece of code to see what happends :)

        Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

        1 Reply Last reply
        0
        • M md_azy

          Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

          A Offline
          A Offline
          Arun Jacob
          wrote on last edited by
          #4

          In your case check if the connection state is open before closing it. :)

          Arun Jacob http://codepronet.blogspot.com/

          1 Reply Last reply
          0
          • M md_azy

            Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #5

            Specifically for your example, you could use a try/catch inside finally, thus:

            try {
            // do something
            // with con
            }
            catch (Exception ex) {
            // Ya boo sucks!
            }
            finally {
            try {
            con.close();
            }
            catch (Exception ex) {
            // unable to close it but must let go!
            }
            finally {
            // finally, finally release the reference
            con = null;
            }
            }

            Easy (and with tags too)


            Panic, Chaos, Destruction. My work here is done.

            P H 2 Replies Last reply
            0
            • N Nagy Vilmos

              Specifically for your example, you could use a try/catch inside finally, thus:

              try {
              // do something
              // with con
              }
              catch (Exception ex) {
              // Ya boo sucks!
              }
              finally {
              try {
              con.close();
              }
              catch (Exception ex) {
              // unable to close it but must let go!
              }
              finally {
              // finally, finally release the reference
              con = null;
              }
              }

              Easy (and with tags too)


              Panic, Chaos, Destruction. My work here is done.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              I'd recommend using a guard clause instead. I know you've answered the OP here, but it's better to do the following:

              try
              {
              }
              catch (...)
              {
              }
              finally
              {
              if (conn.Open)
              conn.Close();
              }

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              N 1 Reply Last reply
              0
              • P Pete OHanlon

                I'd recommend using a guard clause instead. I know you've answered the OP here, but it's better to do the following:

                try
                {
                }
                catch (...)
                {
                }
                finally
                {
                if (conn.Open)
                conn.Close();
                }

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #7

                Probably better [he says after checking that is exactly what I have done] but I do have the closure in the finally block inside it's own try/catch.


                Panic, Chaos, Destruction. My work here is done.

                1 Reply Last reply
                0
                • N Nagy Vilmos

                  Specifically for your example, you could use a try/catch inside finally, thus:

                  try {
                  // do something
                  // with con
                  }
                  catch (Exception ex) {
                  // Ya boo sucks!
                  }
                  finally {
                  try {
                  con.close();
                  }
                  catch (Exception ex) {
                  // unable to close it but must let go!
                  }
                  finally {
                  // finally, finally release the reference
                  con = null;
                  }
                  }

                  Easy (and with tags too)


                  Panic, Chaos, Destruction. My work here is done.

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  Wouldn't

                  using (whateverConnection conn = new whateverConnection())
                  {
                    try
                    {
                      // do stuff
                    }
                    catch
                    {
                      // complain a lot
                    }
                  }
                  

                  be a better approach for the OP?

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  1 Reply Last reply
                  0
                  • M md_azy

                    Hi, What will happen if exception is happening in finally black. for example finally { //i am closing connection object here . con.close() ; //but connection is not opened means this will throw the exception . What will happen for this Exception } Thanks in advance

                    C Offline
                    C Offline
                    carlecomm
                    wrote on last edited by
                    #9

                    finally { if(con != null) con.close() ; }

                    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