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. Good vs Bad

Good vs Bad

Scheduled Pinned Locked Moved C#
questionvisual-studio
11 Posts 6 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.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    Sample 1: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { continue; } } Sample 2: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { // Left blank } } Question: In sample 1 we have "continue;" in catch block and in Sample 2 we have NOT written anything. Are they doing the same thing? If yes, which one is better practice?

    P L D S 4 Replies Last reply
    0
    • N NJdotnetdev

      Sample 1: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { continue; } } Sample 2: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { // Left blank } } Question: In sample 1 we have "continue;" in catch block and in Sample 2 we have NOT written anything. Are they doing the same thing? If yes, which one is better practice?

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

      They are both various shades of bad practice. You're just catching ANY exception here, without actually bothering to do anything about it. In both cases, the behaviour is the same - the loop keeps on iterating, but the reality is that you have just consumed something that has gone wrong. That is bad practice, so I would urge you to do neither case. Exceptions are thrown for a reason. Only catch them if you intend to do something with them, such as logging or you have logic in there to mitigate the effect, e.g. you might have some retry functionality that you need to use. Ignoring exceptions is the wrong thing to do.

      N L 2 Replies Last reply
      0
      • P Pete OHanlon

        They are both various shades of bad practice. You're just catching ANY exception here, without actually bothering to do anything about it. In both cases, the behaviour is the same - the loop keeps on iterating, but the reality is that you have just consumed something that has gone wrong. That is bad practice, so I would urge you to do neither case. Exceptions are thrown for a reason. Only catch them if you intend to do something with them, such as logging or you have logic in there to mitigate the effect, e.g. you might have some retry functionality that you need to use. Ignoring exceptions is the wrong thing to do.

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Thanks for your reply. I am catching InvalidOperationException instead of general exception (will update the question). However, we have plans to get logging in catch block instead of continue or blank. The only reason I wanted to ask this, because I wanted to know if writing continue is bad instead of leaving it blank. --> Our aim was to not do anything for the iteration where an exception is thrown.

        OriginalGriffO 1 Reply Last reply
        0
        • P Pete OHanlon

          They are both various shades of bad practice. You're just catching ANY exception here, without actually bothering to do anything about it. In both cases, the behaviour is the same - the loop keeps on iterating, but the reality is that you have just consumed something that has gone wrong. That is bad practice, so I would urge you to do neither case. Exceptions are thrown for a reason. Only catch them if you intend to do something with them, such as logging or you have logic in there to mitigate the effect, e.g. you might have some retry functionality that you need to use. Ignoring exceptions is the wrong thing to do.

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

          +5 Small addition: If you catch Exceptions to log them or to do anything else that doesn't include mitigating the error, re-throw the exeption at the end of the catch-Block, either with "throw;" to re-throw the exact same Exception or by throwing a custom Exception and providing the original Exception as an InnerException to the constructor. See here: Anti-Patterns and the Solutions[^] especially the part "Error Hiding Anti-Pattern".

          1 Reply Last reply
          0
          • N NJdotnetdev

            Thanks for your reply. I am catching InvalidOperationException instead of general exception (will update the question). However, we have plans to get logging in catch block instead of continue or blank. The only reason I wanted to ask this, because I wanted to know if writing continue is bad instead of leaving it blank. --> Our aim was to not do anything for the iteration where an exception is thrown.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Leaving it blank is bad: it "swallows" the exception, so that nobody ever knows it has happened. So the underlying problem never gets fixed until it has reached massive proportions. If you are going to add logging later, then re-throw the exception (so you know it occurred in development) and add a TODO to the code to remind you to fix it:

            catch(InvalidOperationException ex)
            {
            // TODO Log exception
            throw;
            }

            Adding the TODO means it comes up in the Visual Studio Task List[^] automatically.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            N 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Leaving it blank is bad: it "swallows" the exception, so that nobody ever knows it has happened. So the underlying problem never gets fixed until it has reached massive proportions. If you are going to add logging later, then re-throw the exception (so you know it occurred in development) and add a TODO to the code to remind you to fix it:

              catch(InvalidOperationException ex)
              {
              // TODO Log exception
              throw;
              }

              Adding the TODO means it comes up in the Visual Studio Task List[^] automatically.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              N Offline
              N Offline
              NJdotnetdev
              wrote on last edited by
              #6

              Thanks Everyone for the information.

              1 Reply Last reply
              0
              • N NJdotnetdev

                Sample 1: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { continue; } } Sample 2: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { // Left blank } } Question: In sample 1 we have "continue;" in catch block and in Sample 2 we have NOT written anything. Are they doing the same thing? If yes, which one is better practice?

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

                I would think hard before even trying to put a try ... catch block "inside" a foreach loop. Exception handling can be expensive. If you were looping "thousands" of times, that would potentially mean "thousands" of exceptions. An exception "inside" a loop, generally doesn't happen just once because the data / processing requirements are usually similar. (IMO) One generally wraps a loop "inside" a try ... catch block; not the other way around; in which case, to "continue or not" is a non-issue. Repeated exceptions in a loop are a symptom of bigger problems that should be solved; not just "handled". Exceptions are for "exceptions"; not general error handling.

                1 Reply Last reply
                0
                • N NJdotnetdev

                  Sample 1: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { continue; } } Sample 2: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { // Left blank } } Question: In sample 1 we have "continue;" in catch block and in Sample 2 we have NOT written anything. Are they doing the same thing? If yes, which one is better practice?

                  D Offline
                  D Offline
                  deepankarbhatnagar
                  wrote on last edited by
                  #8

                  In catch, continue will automatically work so no need to add the keyword there.

                  hi

                  L 2 Replies Last reply
                  0
                  • D deepankarbhatnagar

                    In catch, continue will automatically work so no need to add the keyword there.

                    hi

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

                    But it is still a very bad (stupid) thing to do.

                    1 Reply Last reply
                    0
                    • D deepankarbhatnagar

                      In catch, continue will automatically work so no need to add the keyword there.

                      hi

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

                      If one does not include the "continue" then execution will continue after the catch-block, not at the start of the loop.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                      1 Reply Last reply
                      0
                      • N NJdotnetdev

                        Sample 1: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { continue; } } Sample 2: foreach(int i in integerList) { try { // Do something which might throw exception } catch(InvalidOperationException) { // Left blank } } Question: In sample 1 we have "continue;" in catch block and in Sample 2 we have NOT written anything. Are they doing the same thing? If yes, which one is better practice?

                        S Offline
                        S Offline
                        Subramanyam Shankar
                        wrote on last edited by
                        #11

                        Apart from what ever has been answered it is a good practice to catch the general exception also. In both the catch blocks you have to do 2 things log an exception as well as Display the exception to the user. But my suggestion is if you can avoid exceptions that would be even better so have proper checks where ever required like null check etc etc.

                        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