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. Try/Catch Opinions

Try/Catch Opinions

Scheduled Pinned Locked Moved C#
databasequestiondiscussion
27 Posts 11 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
    mjackson11
    wrote on last edited by
    #1

    I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

    P N L E L 7 Replies Last reply
    0
    • M mjackson11

      I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      mjackson11 wrote:

      Is there a lot of overhead associated with these?

      No. Performance implications of Exceptions in .NET[^] Exception Handling Best Practices in .NET[^]

      1 Reply Last reply
      0
      • M mjackson11

        I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        IMO if a condition can be expected and tested for then it is not an exception and you shouldn't rely on exception handling.


        No comment

        P L P 3 Replies Last reply
        0
        • N Not Active

          IMO if a condition can be expected and tested for then it is not an exception and you shouldn't rely on exception handling.


          No comment

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Yes, but on some other hand somewhere... continually testing for a rare state wastes cycles.

          N 1 Reply Last reply
          0
          • N Not Active

            IMO if a condition can be expected and tested for then it is not an exception and you shouldn't rely on exception handling.


            No comment

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

            IMO you should stay away from religious arguments. By the time you're finished checking all the conditions, I could have the deleted the file that you're reading.

            Bastard Programmer from Hell :suss:

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              Yes, but on some other hand somewhere... continually testing for a rare state wastes cycles.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Agreed. There is a difference between rare, unexpected conditions and common, expected conditions.

              PIEBALDconsult wrote:

              some other hand somewhere

              Did you forget where your other hand went? Or are you using someone else's hand? ;P


              No comment

              1 Reply Last reply
              0
              • M mjackson11

                I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

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

                mjackson11 wrote:

                Is there a better way to do this than having a block in each method?

                Yup, you can hook an event that catches all unhandled exceptions[^]. There you can log it, without having the need for a try-catch everywhere. Log everything you encounter, and handle those using try-catch blocks; some will be bugs, some will be exceptions that need a) user interaction (file in use, database dead), b) no user interaction. Handle what you can and log everything.

                Bastard Programmer from Hell :suss:

                J 1 Reply Last reply
                0
                • L Lost User

                  IMO you should stay away from religious arguments. By the time you're finished checking all the conditions, I could have the deleted the file that you're reading.

                  Bastard Programmer from Hell :suss:

                  S Offline
                  S Offline
                  SledgeHammer01
                  wrote on last edited by
                  #8

                  This. Writing "bullet proof" code that handles every case from day one, I've learned is a waste of your time and CPU cycles and does nothing but piss off your boss who is wondering why it is taking you so long. Write your first pass with general case error handling and add the obscure corner cases as you come across them in testing. However, structuring your code with the intention of handling these corner cases down the road would behoove you. What I mean by that is... well, one of the projects I'm working on is a project that my boss implemented 100%, and he is "from the street" and doesn't believe in structure and design, so there is SQL code sprinkled pretty much every where. Had he packaged all his SQL code into a single DAL, it would have been much easier to debug and expand. The project that I wrote 100% from scratch, I did exactly that... yeah, it didn't handle every corner case from day one, but as I found the cases, I only had to change 1 or 2 places. I'm mentioning this because it sounds like you have your external access stuff sprinkled everywhere throughout the GUI :).

                  1 Reply Last reply
                  0
                  • M mjackson11

                    I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

                    E Offline
                    E Offline
                    Ennis Ray Lynch Jr
                    wrote on last edited by
                    #9

                    My rule of thumb, I never catch an error I can't handle. And I never try something when there is an associated way to guarantee success. There are some people that clamor about correct code causing missed deadlines. Anecdotaly, successful programmers never complain about how much time it takes to write code correctly.

                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                    P 1 Reply Last reply
                    0
                    • E Ennis Ray Lynch Jr

                      My rule of thumb, I never catch an error I can't handle. And I never try something when there is an associated way to guarantee success. There are some people that clamor about correct code causing missed deadlines. Anecdotaly, successful programmers never complain about how much time it takes to write code correctly.

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                      P Online
                      P Online
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Hear hear! "If you don't have time to do it right, when will you have time to do it over?"

                      1 Reply Last reply
                      0
                      • N Not Active

                        IMO if a condition can be expected and tested for then it is not an exception and you shouldn't rely on exception handling.


                        No comment

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

                        Indeed, where it's appropriate, a simple check can often save time. Ultimately, as you progress, you end up building a handy set of utility methods that you can use, for instance here's a simplified version of a file create that you could use:

                        public static FileStream TryCreateFile(string fileToCreate)
                        {
                        string directoryName = Path.GetDirectoryName(fileToCreate);
                        if (!Directory.Exists(directoryName))
                        Directory.CreateDirectory(directoryName);
                        return File.Create(fileToCreate);
                        }

                        Forgive your enemies - it messes with their heads

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                        P L B 3 Replies Last reply
                        0
                        • P Pete OHanlon

                          Indeed, where it's appropriate, a simple check can often save time. Ultimately, as you progress, you end up building a handy set of utility methods that you can use, for instance here's a simplified version of a file create that you could use:

                          public static FileStream TryCreateFile(string fileToCreate)
                          {
                          string directoryName = Path.GetDirectoryName(fileToCreate);
                          if (!Directory.Exists(directoryName))
                          Directory.CreateDirectory(directoryName);
                          return File.Create(fileToCreate);
                          }

                          Forgive your enemies - it messes with their heads

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

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

                          How does that save time? Delete will check for the existence of the file a second time. Edit: Or is that just a bad example? "If the file to be deleted does not exist, no exception is thrown."

                          P 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            Indeed, where it's appropriate, a simple check can often save time. Ultimately, as you progress, you end up building a handy set of utility methods that you can use, for instance here's a simplified version of a file create that you could use:

                            public static FileStream TryCreateFile(string fileToCreate)
                            {
                            string directoryName = Path.GetDirectoryName(fileToCreate);
                            if (!Directory.Exists(directoryName))
                            Directory.CreateDirectory(directoryName);
                            return File.Create(fileToCreate);
                            }

                            Forgive your enemies - it messes with their heads

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

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

                            Wouldn't be so handy if you'd use it to clear a folder. If you recently fetched the folders' contents, chances are that the files still exist.

                            Bastard Programmer from Hell :suss:

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              How does that save time? Delete will check for the existence of the file a second time. Edit: Or is that just a bad example? "If the file to be deleted does not exist, no exception is thrown."

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

                              It was actually a bad example - I'll update it with the example I was actually thinking of.

                              Forgive your enemies - it messes with their heads

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                              L 1 Reply Last reply
                              0
                              • M mjackson11

                                I have several small programs that scrap input and load it into a database. Since much of the code relies on external resources being ready, a lot of my code ends up being in try/catch blocks. Is there a lot of overhead associated with these? I have tried to consolidate interaction with external sites and db's so there are a minimal number. Is there a better way to do this than having a block in each method? Thx Mark Jackson

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                It depends on the situation. 1. If it is an all-good-or-not situation, then it deserves a single overall try-catch. 2. If you want to extract as much as possible, then you need very granular error checking (if possible) or try-catching (if no error peeking is possible). 3. And if you can't handle the exception(s), then it falls back to either #1 or #2 anyway. :)

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  It was actually a bad example - I'll update it with the example I was actually thinking of.

                                  Forgive your enemies - it messes with their heads

                                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  I'm afraid this isn't your day; you failed at improving the example. Directory.CreateDirectory() does nothing when the folder already exists, so there is no point checking first. :-D

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  P 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    I'm afraid this isn't your day; you failed at improving the example. Directory.CreateDirectory() does nothing when the folder already exists, so there is no point checking first. :-D

                                    Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                    Actually, in this particular case, there is - the IL is slightly different internally, and it saves clock cycles on the test in CreateDirectory. That's the reason it's in. The important part is actually the second part - hence the point of the example.

                                    Forgive your enemies - it messes with their heads

                                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                    L 1 Reply Last reply
                                    0
                                    • P Pete OHanlon

                                      Actually, in this particular case, there is - the IL is slightly different internally, and it saves clock cycles on the test in CreateDirectory. That's the reason it's in. The important part is actually the second part - hence the point of the example.

                                      Forgive your enemies - it messes with their heads

                                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #18

                                      Ah. I never looked at the IL, I was assuming they were smart enough to make the test inside CreateDirectory() as efficient as the one inside Exists(). :doh:

                                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                                      P 1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        Ah. I never looked at the IL, I was assuming they were smart enough to make the test inside CreateDirectory() as efficient as the one inside Exists(). :doh:

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                        Why do I feel the need to go "Helloooooo. Microsoft."?

                                        Forgive your enemies - it messes with their heads

                                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                        L 1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          Why do I feel the need to go "Helloooooo. Microsoft."?

                                          Forgive your enemies - it messes with their heads

                                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          I don't know. Besides, I prefer How questions over Why questions any day. :)

                                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                                          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