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. Nested try/catch blocks

Nested try/catch blocks

Scheduled Pinned Locked Moved C#
helpdatabasesysadminquestion
12 Posts 7 Posters 1 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.
  • H Helfdane

    I'm somewhat hesitating about using nested try/catch blocks. The situation which makes me think about is the following situation. Some sample code to illustrate:

    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlTransaction trans = conn.BeginTransaction();

    try
    {
    string query = "query goes here";
    SqlCommand command = new SqlCommand(query, conn, trans);
    // add parameters
    command.ExecuteNonQuery();
    trans.Commit();
    }
    catch
    {
    trans.Rollback();
    }
    finally
    {
    if(conn != null)
    {
    conn.Close();
    conn.Dispose();
    }
    }

    This block has 1 try/catch block but the problem resides in the "conn.Open();" line. When there is a network error the conn.Open() will throw an exception. Putting the conn.Open() in the try/catch block will invalidate the code which now resides in the catch/finally clauses. So I can use 2 sequential try/catch blocks or a nexted construction. What do you people think?

    The consumer isn't a moron; she is your wife.

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    It won't invalidate anything. You can just put an (if conn != null) type block at the start of the second try/catch, and then your finally block will work fine. Indeed, command.ExecuteNonQuery would just force the catch and finally, if conn was null. I'm also not sure why conn couldn't be in the existing try block, just declare trans outside it, and begin it, inside.

    Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

    H V 2 Replies Last reply
    0
    • C Christian Graus

      It won't invalidate anything. You can just put an (if conn != null) type block at the start of the second try/catch, and then your finally block will work fine. Indeed, command.ExecuteNonQuery would just force the catch and finally, if conn was null. I'm also not sure why conn couldn't be in the existing try block, just declare trans outside it, and begin it, inside.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      H Offline
      H Offline
      Helfdane
      wrote on last edited by
      #3

      Ah yes, you're right. That would make it a viable solution indeed. *reminds himself not to start coding too early in the morning* ;)

      The consumer isn't a moron; she is your wife.

      D 1 Reply Last reply
      0
      • H Helfdane

        Ah yes, you're right. That would make it a viable solution indeed. *reminds himself not to start coding too early in the morning* ;)

        The consumer isn't a moron; she is your wife.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #4

        Helfdane wrote:

        reminds himself not to start coding too early in the morning

        The best time is after :java: but before :beer:

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        H 1 Reply Last reply
        0
        • D DaveyM69

          Helfdane wrote:

          reminds himself not to start coding too early in the morning

          The best time is after :java: but before :beer:

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          H Offline
          H Offline
          Helfdane
          wrote on last edited by
          #5

          That would mean my evening diner as I'm sitting next to the coffee machine ;)

          The consumer isn't a moron; she is your wife.

          1 Reply Last reply
          0
          • H Helfdane

            I'm somewhat hesitating about using nested try/catch blocks. The situation which makes me think about is the following situation. Some sample code to illustrate:

            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            SqlTransaction trans = conn.BeginTransaction();

            try
            {
            string query = "query goes here";
            SqlCommand command = new SqlCommand(query, conn, trans);
            // add parameters
            command.ExecuteNonQuery();
            trans.Commit();
            }
            catch
            {
            trans.Rollback();
            }
            finally
            {
            if(conn != null)
            {
            conn.Close();
            conn.Dispose();
            }
            }

            This block has 1 try/catch block but the problem resides in the "conn.Open();" line. When there is a network error the conn.Open() will throw an exception. Putting the conn.Open() in the try/catch block will invalidate the code which now resides in the catch/finally clauses. So I can use 2 sequential try/catch blocks or a nexted construction. What do you people think?

            The consumer isn't a moron; she is your wife.

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

            Sequencial try/catches would be better. Something like:

            SqlConnection conn;
            SqlTransaction trans;

            try {
            conn = new SqlConnection(connectionString);
            conn.Open();
            SqlTransaction trans = conn.BeginTransaction();
            }
            catch (SomeTypeOfException ex) {
            // deal with exception
            }
            finally {
            // transaction as well??

            if (conn != null) {
            conn.Close();
            conn.Dispose();
            }
            }

            // if we've got a trasnaction carry on:
            if (trans != null) {
            try {
            string query = "query goes here";
            SqlCommand command = new SqlCommand(query, conn, trans);
            // add parameters
            command.ExecuteNonQuery();
            trans.Commit();
            }
            catch (SomeOtherException ex) {
            // deal with exception
            trans.Rollback();
            }
            finally {
            if (conn != null) {
            conn.Close();
            conn.Dispose();
            }
            }
            }


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

            H P 2 Replies Last reply
            0
            • N Nagy Vilmos

              Sequencial try/catches would be better. Something like:

              SqlConnection conn;
              SqlTransaction trans;

              try {
              conn = new SqlConnection(connectionString);
              conn.Open();
              SqlTransaction trans = conn.BeginTransaction();
              }
              catch (SomeTypeOfException ex) {
              // deal with exception
              }
              finally {
              // transaction as well??

              if (conn != null) {
              conn.Close();
              conn.Dispose();
              }
              }

              // if we've got a trasnaction carry on:
              if (trans != null) {
              try {
              string query = "query goes here";
              SqlCommand command = new SqlCommand(query, conn, trans);
              // add parameters
              command.ExecuteNonQuery();
              trans.Commit();
              }
              catch (SomeOtherException ex) {
              // deal with exception
              trans.Rollback();
              }
              finally {
              if (conn != null) {
              conn.Close();
              conn.Dispose();
              }
              }
              }


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

              H Offline
              H Offline
              Helfdane
              wrote on last edited by
              #7

              For the situation above, Christian Graus gave a good hint on a potential solution, and so do you. But in general, why is sequential better than nested?

              The consumer isn't a moron; she is your wife.

              N 1 Reply Last reply
              0
              • N Nagy Vilmos

                Sequencial try/catches would be better. Something like:

                SqlConnection conn;
                SqlTransaction trans;

                try {
                conn = new SqlConnection(connectionString);
                conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                }
                catch (SomeTypeOfException ex) {
                // deal with exception
                }
                finally {
                // transaction as well??

                if (conn != null) {
                conn.Close();
                conn.Dispose();
                }
                }

                // if we've got a trasnaction carry on:
                if (trans != null) {
                try {
                string query = "query goes here";
                SqlCommand command = new SqlCommand(query, conn, trans);
                // add parameters
                command.ExecuteNonQuery();
                trans.Commit();
                }
                catch (SomeOtherException ex) {
                // deal with exception
                trans.Rollback();
                }
                finally {
                if (conn != null) {
                conn.Close();
                conn.Dispose();
                }
                }
                }


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

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

                doesn't the first finally block close the connection resulting in exception when trying to execute a query in the second try block; and since the second catch block also uses Rollback, doesn't that throw another exception every time the function is called! :omg:

                regards :)

                N 1 Reply Last reply
                0
                • C Christian Graus

                  It won't invalidate anything. You can just put an (if conn != null) type block at the start of the second try/catch, and then your finally block will work fine. Indeed, command.ExecuteNonQuery would just force the catch and finally, if conn was null. I'm also not sure why conn couldn't be in the existing try block, just declare trans outside it, and begin it, inside.

                  Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                  V Offline
                  V Offline
                  Vikram A Punathambekar
                  wrote on last edited by
                  #9

                  Christian Graus wrote:

                  I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                  :omg: Now I've seen everything! :laugh:

                  Cheers, Vikram.

                  Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.


                  Carpe Diem.

                  1 Reply Last reply
                  0
                  • P PandemoniumPasha

                    doesn't the first finally block close the connection resulting in exception when trying to execute a query in the second try block; and since the second catch block also uses Rollback, doesn't that throw another exception every time the function is called! :omg:

                    regards :)

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

                    :-O I didn't say it was perfect.


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

                    1 Reply Last reply
                    0
                    • H Helfdane

                      For the situation above, Christian Graus gave a good hint on a potential solution, and so do you. But in general, why is sequential better than nested?

                      The consumer isn't a moron; she is your wife.

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

                      You put a try catch around the possible/probable points of failure and catch SPECIFIC errors. Catch and ignore is, generally, not a good idea. Ignoring a specific case can be okay so long as you impliment a valid recovery. ps. as pointed out the first finally is cr@p.


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

                      1 Reply Last reply
                      0
                      • H Helfdane

                        I'm somewhat hesitating about using nested try/catch blocks. The situation which makes me think about is the following situation. Some sample code to illustrate:

                        SqlConnection conn = new SqlConnection(connectionString);
                        conn.Open();
                        SqlTransaction trans = conn.BeginTransaction();

                        try
                        {
                        string query = "query goes here";
                        SqlCommand command = new SqlCommand(query, conn, trans);
                        // add parameters
                        command.ExecuteNonQuery();
                        trans.Commit();
                        }
                        catch
                        {
                        trans.Rollback();
                        }
                        finally
                        {
                        if(conn != null)
                        {
                        conn.Close();
                        conn.Dispose();
                        }
                        }

                        This block has 1 try/catch block but the problem resides in the "conn.Open();" line. When there is a network error the conn.Open() will throw an exception. Putting the conn.Open() in the try/catch block will invalidate the code which now resides in the catch/finally clauses. So I can use 2 sequential try/catch blocks or a nexted construction. What do you people think?

                        The consumer isn't a moron; she is your wife.

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

                        Or both? :-D

                        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