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. Other Discussions
  3. The Weird and The Wonderful
  4. If it fails, try again

If it fails, try again

Scheduled Pinned Locked Moved The Weird and The Wonderful
database
37 Posts 19 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.
  • G gjp1311

    Besides the problem of repeating the same thing inside a catch, the other big problem is that the second time also throws an error sometimes. The "easy" fix was to put another try and catch block inside that catch, until I could understand what the hell was happening in that code :laugh:

    G Offline
    G Offline
    Graham Coulby
    wrote on last edited by
    #6

    This calls for a do-while! xD

    bool failure = true;
    do
    {
    try
    {
    //connect to db

        failure = false;
    }
    catch(Exception e)
    {
        failure = true;
    }
    

    } while (failure);

    D 1 Reply Last reply
    0
    • G Graham Coulby

      This calls for a do-while! xD

      bool failure = true;
      do
      {
      try
      {
      //connect to db

          failure = false;
      }
      catch(Exception e)
      {
          failure = true;
      }
      

      } while (failure);

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #7

      See [Einstein's definition of insanity](https://www.brainyquote.com/quotes/quotes/a/alberteins133991.html).

      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

      K P R 3 Replies Last reply
      0
      • Sander RosselS Sander Rossel

        I've seen and written code like that. One time there was a bug in a big ass Oracle procedure that made it return no records on the first try, but did the right thing after that (don't know what it was, we're talking about 100's of lines of Oracle code, a horror in its own right!). Once had an issue with a rather unstable connection that often made it fail the first time. Same thing for timeouts. Timeout on first try, instant result on the second try. While retrying isn't really a solution it may get the desired result with very few trouble. Especially useful when it has to work NOW and the real issue is not so easy to fix (or not yours to fix).

        Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

        T Offline
        T Offline
        the Kris
        wrote on last edited by
        #8

        If that was the problem the author of that code tried to 'fix', (s)he should've added a comment to indicate that.

        Sander RosselS 1 Reply Last reply
        0
        • T the Kris

          If that was the problem the author of that code tried to 'fix', (s)he should've added a comment to indicate that.

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #9

          Even though I consider comments to be one of the evils of programming I agree with you on this one :D

          Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

          J R 2 Replies Last reply
          0
          • G gjp1311

            Besides the problem of repeating the same thing inside a catch, the other big problem is that the second time also throws an error sometimes. The "easy" fix was to put another try and catch block inside that catch, until I could understand what the hell was happening in that code :laugh:

            Sander RosselS Offline
            Sander RosselS Offline
            Sander Rossel
            wrote on last edited by
            #10

            I'm assuming the programmer assumed the first try could go wrong and handles exceptions in the caller. The retry is really just that, retry and if it fails just fail (like it could fail the first time, under normal conditions, as well). Repeating two simple lines of code (not even business logic) isn't really a problem.

            Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

            1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              Even though I consider comments to be one of the evils of programming I agree with you on this one :D

              Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

              J Offline
              J Offline
              Jim_Snyder
              wrote on last edited by
              #11

              If your comments do not fill at least one third of your code, copy & paste until it does!

              Sander RosselS 1 Reply Last reply
              0
              • Sander RosselS Sander Rossel

                I've seen and written code like that. One time there was a bug in a big ass Oracle procedure that made it return no records on the first try, but did the right thing after that (don't know what it was, we're talking about 100's of lines of Oracle code, a horror in its own right!). Once had an issue with a rather unstable connection that often made it fail the first time. Same thing for timeouts. Timeout on first try, instant result on the second try. While retrying isn't really a solution it may get the desired result with very few trouble. Especially useful when it has to work NOW and the real issue is not so easy to fix (or not yours to fix).

                Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                K Offline
                K Offline
                kmoorevs
                wrote on last edited by
                #12

                Sander Rossel wrote:

                Once had an issue with a rather unstable connection that often made it fail the first time.

                This reminds me of a funny story where a customer kept having intermittent issues with our software...database connections kept timing out. :confused: Every time the guy would call having problems, I would remote in, and the problem wouldn't happen. Luckily, they were within driving distance, so I went onsite to try and sort it out. The end user's office was across the driveway to a shipping dock and warehouse. The database server was located in the warehouse office, and the network connection between the two offices was a p2p (line of sight) wireless setup. (apparently a short term solution while they were constructing new offices) When I arrived, there wasn't a lot going on...the software worked without fail. After a while, a truck arrived and began backing into the dock...and suddenly, the database connection was gone! :omg: For weeks, the customer never made the 'connection'! Luckily, the fix was simply raising the p2p units by a few more feet.

                "Go forth into the source" - Neal Morse

                Sander RosselS 1 Reply Last reply
                0
                • D Daniel Pfeffer

                  See [Einstein's definition of insanity](https://www.brainyquote.com/quotes/quotes/a/alberteins133991.html).

                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                  K Offline
                  K Offline
                  Kirk 10389821
                  wrote on last edited by
                  #13

                  I don't think this counts, because they are EXPECTING different results once they have an exception...

                  1 Reply Last reply
                  0
                  • K kmoorevs

                    Sander Rossel wrote:

                    Once had an issue with a rather unstable connection that often made it fail the first time.

                    This reminds me of a funny story where a customer kept having intermittent issues with our software...database connections kept timing out. :confused: Every time the guy would call having problems, I would remote in, and the problem wouldn't happen. Luckily, they were within driving distance, so I went onsite to try and sort it out. The end user's office was across the driveway to a shipping dock and warehouse. The database server was located in the warehouse office, and the network connection between the two offices was a p2p (line of sight) wireless setup. (apparently a short term solution while they were constructing new offices) When I arrived, there wasn't a lot going on...the software worked without fail. After a while, a truck arrived and began backing into the dock...and suddenly, the database connection was gone! :omg: For weeks, the customer never made the 'connection'! Luckily, the fix was simply raising the p2p units by a few more feet.

                    "Go forth into the source" - Neal Morse

                    Sander RosselS Offline
                    Sander RosselS Offline
                    Sander Rossel
                    wrote on last edited by
                    #14

                    Customers do that :laugh: I have a similar story with a printer. The user pressed the print button to print somewhere around 100 to 150 invoices. It often happened that the printing stopped after 50 or so invoices. And always at the end of the day (because that's when they printed invoices). So I was testing my software, looking for bugs, writing "fixes" for "what could be it", added additional logging, everything, but I was never able to find the problem. And when I visited it didn't happen, of course. This went on for weeks. Then my manager visited them, for completely unrelated business, and called me "Is that print problem fixed already? I'm about to leave, but they're going to print invoices so I can check on them now." He checked and you'll never believe what their issue was. They clicked the print button and SHUT DOWN THEIR COMPUTER TO GO HOME!!! :wtf: :omg: :~ X| Never did it occur to them that shutting down the computer would stop it from printing. I can tell you I felt like driving a truck over their computers! :laugh:

                    Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                    D D 2 Replies Last reply
                    0
                    • J Jim_Snyder

                      If your comments do not fill at least one third of your code, copy & paste until it does!

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #15

                      We actually had such a rule. Our automated build system would keep giving us errors "At least 25% of a file must be comments." X| After some insistence from my part they disabled that rule :D

                      Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                      J K 3 Replies Last reply
                      0
                      • Sander RosselS Sander Rossel

                        We actually had such a rule. Our automated build system would keep giving us errors "At least 25% of a file must be comments." X| After some insistence from my part they disabled that rule :D

                        Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                        J Offline
                        J Offline
                        Jim_Snyder
                        wrote on last edited by
                        #16

                        Being a rather elderly gentleman, I am very religious in my documentation. I know I will be maintaining my own code and will not remember why I did something. Most of my documentation is the "why" I did something or "how" it works, but occasionally I justify to myself why a particular construct or pathway was chosen. My code is self-documenting as far as it can be, but comments still give me quicker access to the areas that need modified/added. In addition, since the compiler doesn't care about whitespace, I use it systematically to separate blocks of code or related groups of commands from the rest. Now when it comes to resources, I will use whatever the language supplies with the only limitations being running efficiently and being easy to read. I am as likely to use a generic list as I am a hash table depending on what is required.

                        1 Reply Last reply
                        0
                        • Sander RosselS Sander Rossel

                          We actually had such a rule. Our automated build system would keep giving us errors "At least 25% of a file must be comments." X| After some insistence from my part they disabled that rule :D

                          Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                          J Offline
                          J Offline
                          Jim_Snyder
                          wrote on last edited by
                          #17

                          Congratulations on successfully disabling the rule!

                          1 Reply Last reply
                          0
                          • G gjp1311

                            Beacause it will surely work a 100% the second time :-\

                            SqlCommand cmd = new SqlCommand(query,conn);
                            SqlDataReader reader = null;
                            try
                            {
                            reader = cmd.ExecuteReader();
                            }
                            catch(InvalidOperationException e)
                            {
                            cmd = new SqlCommand(query,conn);
                            reader = cmd.ExecuteReader();
                            }

                            T Online
                            T Online
                            theoldfool
                            wrote on last edited by
                            #18

                            Probably not much help when a parachute does that.

                            Arguing with a woman is like reading the Software License Agreement. In the end, you ignore everything and click "I agree". Anonymous

                            1 Reply Last reply
                            0
                            • Sander RosselS Sander Rossel

                              Customers do that :laugh: I have a similar story with a printer. The user pressed the print button to print somewhere around 100 to 150 invoices. It often happened that the printing stopped after 50 or so invoices. And always at the end of the day (because that's when they printed invoices). So I was testing my software, looking for bugs, writing "fixes" for "what could be it", added additional logging, everything, but I was never able to find the problem. And when I visited it didn't happen, of course. This went on for weeks. Then my manager visited them, for completely unrelated business, and called me "Is that print problem fixed already? I'm about to leave, but they're going to print invoices so I can check on them now." He checked and you'll never believe what their issue was. They clicked the print button and SHUT DOWN THEIR COMPUTER TO GO HOME!!! :wtf: :omg: :~ X| Never did it occur to them that shutting down the computer would stop it from printing. I can tell you I felt like driving a truck over their computers! :laugh:

                              Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                              D Offline
                              D Offline
                              Daniel Wilianto
                              wrote on last edited by
                              #19

                              True story. Some users seems to assume that printers have internal memory which would keep our queued documents. This assumption comes from a fact that printer keeps printing even when they closed our software. They don't know that these queued documents are stored on operating system's printer spooler service. Some colleagues once asked me, why did her printer stop printing? I who was in the middle of serious work mode, glanced over at her desk and yelled, you turned off your computer, that's why!

                              R 1 Reply Last reply
                              0
                              • G gjp1311

                                Beacause it will surely work a 100% the second time :-\

                                SqlCommand cmd = new SqlCommand(query,conn);
                                SqlDataReader reader = null;
                                try
                                {
                                reader = cmd.ExecuteReader();
                                }
                                catch(InvalidOperationException e)
                                {
                                cmd = new SqlCommand(query,conn);
                                reader = cmd.ExecuteReader();
                                }

                                K Offline
                                K Offline
                                kalberts
                                wrote on last edited by
                                #20

                                If "try again!" always was silly coding practice, the the concept of "busy waiting" would not be known. Busy waiting does have its place, especially in embedded systems where the processor has nothing else to do. Another situation is where several activities are competing for the same resource, but they hold the resource comparatively briefly. As long as you can modify all the competitors, you could program the queueing mechanism, where a process trying to access a busy resource is sent to sleep in a queue and woken up when it gets its turn. Often you can only modify some of the competitors. The effort of implementing a queue mechanism may be high. If the risk of collision is small, it simply doesn't pay. I have fairly recently programmed a couple "try again" cases: On a quite heavily loaded file system, the number of exceptions due to confliciting accesses was higher than desired. So, when making modifications to a set of files, I make one try for each. Those failing are put into a list for retrying, and a second attempt is made once the first round was completed. Only after a second try, the user is notified. We went from too many access collisions to almost none. Sometimes, the second try came too fast, and the file was still busy. So I added a 5 ms sleep before starting a second round, and after that I haven't seen a single collision. This "try again" code is next to trivial, and it does solve a real problem. So I cannot agree that the strategy is silly in every case. Sometimes it makes perfect sense.

                                K R 2 Replies Last reply
                                0
                                • Sander RosselS Sander Rossel

                                  We actually had such a rule. Our automated build system would keep giving us errors "At least 25% of a file must be comments." X| After some insistence from my part they disabled that rule :D

                                  Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                                  K Offline
                                  K Offline
                                  kalberts
                                  wrote on last edited by
                                  #21

                                  I don't like the way it is phrased. If you phrase it: "At least 25% of the lines should be explained by a comment", then I agree. Leaving more than 75% as "self explanatory" is very costly, five years later. I am very fond of end-of-line comments; they can be made short, to the point, and at the right place. If you need something between the software architecture documentation (I often describe "software architecture" as "the structures that will remain the same if you reimplement the system in a different programming language"), a comment block may apply to, say, an entire function, or at least the core part of it. From the moment you compile a source file for the first time and three weeks thereafter, any code is "self explanatory". After two years, not even the code written by yourself qualifies. A classic: Geek & Poke[^]

                                  Sander RosselS 1 Reply Last reply
                                  0
                                  • G gjp1311

                                    Beacause it will surely work a 100% the second time :-\

                                    SqlCommand cmd = new SqlCommand(query,conn);
                                    SqlDataReader reader = null;
                                    try
                                    {
                                    reader = cmd.ExecuteReader();
                                    }
                                    catch(InvalidOperationException e)
                                    {
                                    cmd = new SqlCommand(query,conn);
                                    reader = cmd.ExecuteReader();
                                    }

                                    Richard DeemingR Offline
                                    Richard DeemingR Offline
                                    Richard Deeming
                                    wrote on last edited by
                                    #22

                                    Sometimes it will - for example, the SqlConnection class was updated recently to retry a failed connection, to account for unreliable networks. However, an automatic retry can still have unintended consequences: Try and try again: not always a good idea (at least not for SSMS!) | SQL Server Customer Advisory Team[^]


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                    1 Reply Last reply
                                    0
                                    • K kalberts

                                      If "try again!" always was silly coding practice, the the concept of "busy waiting" would not be known. Busy waiting does have its place, especially in embedded systems where the processor has nothing else to do. Another situation is where several activities are competing for the same resource, but they hold the resource comparatively briefly. As long as you can modify all the competitors, you could program the queueing mechanism, where a process trying to access a busy resource is sent to sleep in a queue and woken up when it gets its turn. Often you can only modify some of the competitors. The effort of implementing a queue mechanism may be high. If the risk of collision is small, it simply doesn't pay. I have fairly recently programmed a couple "try again" cases: On a quite heavily loaded file system, the number of exceptions due to confliciting accesses was higher than desired. So, when making modifications to a set of files, I make one try for each. Those failing are put into a list for retrying, and a second attempt is made once the first round was completed. Only after a second try, the user is notified. We went from too many access collisions to almost none. Sometimes, the second try came too fast, and the file was still busy. So I added a 5 ms sleep before starting a second round, and after that I haven't seen a single collision. This "try again" code is next to trivial, and it does solve a real problem. So I cannot agree that the strategy is silly in every case. Sometimes it makes perfect sense.

                                      K Offline
                                      K Offline
                                      kmoorevs
                                      wrote on last edited by
                                      #23

                                      Member 7989122 wrote:

                                      So I cannot agree that the strategy is silly in every case. Sometimes it makes perfect sense.

                                      :thumbsup: Great examples but not quite the same as the OP where it doesn't make sense to try the same command again right away. This also brought to mind a program I have to migrate database objects...those having a dependency yet to be created fail and get put into a recursive 'try again' queue. :)

                                      "Go forth into the source" - Neal Morse

                                      1 Reply Last reply
                                      0
                                      • K kalberts

                                        I don't like the way it is phrased. If you phrase it: "At least 25% of the lines should be explained by a comment", then I agree. Leaving more than 75% as "self explanatory" is very costly, five years later. I am very fond of end-of-line comments; they can be made short, to the point, and at the right place. If you need something between the software architecture documentation (I often describe "software architecture" as "the structures that will remain the same if you reimplement the system in a different programming language"), a comment block may apply to, say, an entire function, or at least the core part of it. From the moment you compile a source file for the first time and three weeks thereafter, any code is "self explanatory". After two years, not even the code written by yourself qualifies. A classic: Geek & Poke[^]

                                        Sander RosselS Offline
                                        Sander RosselS Offline
                                        Sander Rossel
                                        wrote on last edited by
                                        #24

                                        Depends, most of the code is just properties, setting, getting, some database queries, standard stuff. I always get a little sad when I see comments like:

                                        // Assign the variable.
                                        someVar = someVal;

                                        // Loop through the products.
                                        foreach (Product p in products)

                                        // Save the customer.
                                        customer.Save();

                                        // Save the customer. [notice the error]
                                        product.Save();

                                        Unfortunately, I've only seen comments like that. And because of these kind of comments I came to detest them. You may think I'm overreacting, but those are actual real-life comments I see almost daily. I have 7 years of experience and I have yet to find a single comment that is actually helpful. I rarely comment my code, maybe once or twice when there were some weird side-effects (which is just bad code). I'm at the point that I prefer my code uncommented (especially when it's bad code, people who write bad code also write bad comments). Yeah, comments can't be done right.

                                        Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                                        N 1 Reply Last reply
                                        0
                                        • Sander RosselS Sander Rossel

                                          Depends, most of the code is just properties, setting, getting, some database queries, standard stuff. I always get a little sad when I see comments like:

                                          // Assign the variable.
                                          someVar = someVal;

                                          // Loop through the products.
                                          foreach (Product p in products)

                                          // Save the customer.
                                          customer.Save();

                                          // Save the customer. [notice the error]
                                          product.Save();

                                          Unfortunately, I've only seen comments like that. And because of these kind of comments I came to detest them. You may think I'm overreacting, but those are actual real-life comments I see almost daily. I have 7 years of experience and I have yet to find a single comment that is actually helpful. I rarely comment my code, maybe once or twice when there were some weird side-effects (which is just bad code). I'm at the point that I prefer my code uncommented (especially when it's bad code, people who write bad code also write bad comments). Yeah, comments can't be done right.

                                          Best, Sander arrgh.js - Bringing LINQ to JavaScript SQL Server for C# Developers Succinctly Object-Oriented Programming in C# Succinctly

                                          N Offline
                                          N Offline
                                          Nelek
                                          wrote on last edited by
                                          #25

                                          Have you been reviewing your own code years later? At the beginning I was like you, but then I started having to upgrade my own code... I ended writing comments where needed note: I say where needed, not overall. But... if not sure, the default is... comment, better one too much than one too few.

                                          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                          Sander RosselS K 2 Replies 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