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. The Lounge
  3. Multiple Catch blocks that do the same thing...

Multiple Catch blocks that do the same thing...

Scheduled Pinned Locked Moved The Lounge
helpquestion
34 Posts 12 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.
  • J Offline
    J Offline
    jeeves77
    wrote on last edited by
    #1

    Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

    try
    {
    DoSomething();
    }
    catch (SocketException e)
    {
    Console.WriteLine(e.Message);
    conn.connecting = false;
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    conn.connecting = false;
    }

    What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

    S U OriginalGriffO Mike HankeyM J 9 Replies Last reply
    0
    • J jeeves77

      Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

      try
      {
      DoSomething();
      }
      catch (SocketException e)
      {
      Console.WriteLine(e.Message);
      conn.connecting = false;
      }
      catch (Exception e)
      {
      Console.WriteLine(e.Message);
      conn.connecting = false;
      }

      What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #2

      jeeves77 wrote:

      What purpose in life, universe, code, etc... does a practice like this serve?!

      Zero :laugh:

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      1 Reply Last reply
      0
      • J jeeves77

        Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

        try
        {
        DoSomething();
        }
        catch (SocketException e)
        {
        Console.WriteLine(e.Message);
        conn.connecting = false;
        }
        catch (Exception e)
        {
        Console.WriteLine(e.Message);
        conn.connecting = false;
        }

        What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

        U Offline
        U Offline
        User 11360346
        wrote on last edited by
        #3

        Yes it has purpose. You should see this msdn article. [^] But it wasn't utilized as suppose to.

        OriginalGriffO 1 Reply Last reply
        0
        • J jeeves77

          Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

          try
          {
          DoSomething();
          }
          catch (SocketException e)
          {
          Console.WriteLine(e.Message);
          conn.connecting = false;
          }
          catch (Exception e)
          {
          Console.WriteLine(e.Message);
          conn.connecting = false;
          }

          What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

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

          There is a recommended practice where you catch the minimum exception you can: SocketException instead of Exception for example, but that code's silly. It's possible that he planned to come back and do something different for the SocketException later and never got round to it, but if that's the case the author still needs a thump upside the head... :laugh:

          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

          Z 1 Reply Last reply
          0
          • U User 11360346

            Yes it has purpose. You should see this msdn article. [^] But it wasn't utilized as suppose to.

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

            I looked, I saw: but I didn't see a purpose to having two exception catchers that do the same thing. Yes, if the SocketException catcher logged it differently, or re-tried or similar, and the Exception catcher did a generic log-and-exit instead then it's would have a purpose. But two exceptions (one of which catches everything) that do the same thing? I see no point, except to duplicate code and potentially cause a maintenance problem if the logging mechanism changes.

            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

            U 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              I looked, I saw: but I didn't see a purpose to having two exception catchers that do the same thing. Yes, if the SocketException catcher logged it differently, or re-tried or similar, and the Exception catcher did a generic log-and-exit instead then it's would have a purpose. But two exceptions (one of which catches everything) that do the same thing? I see no point, except to duplicate code and potentially cause a maintenance problem if the logging mechanism changes.

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

              U Offline
              U Offline
              User 11360346
              wrote on last edited by
              #6

              We can't predict looking the above code since we don't know what Dosomething() method exactly do. What we can infer is that there is a socket processing behind since there is a catch exception on it.

              S 1 Reply Last reply
              0
              • J jeeves77

                Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

                try
                {
                DoSomething();
                }
                catch (SocketException e)
                {
                Console.WriteLine(e.Message);
                conn.connecting = false;
                }
                catch (Exception e)
                {
                Console.WriteLine(e.Message);
                conn.connecting = false;
                }

                What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

                Mike HankeyM Offline
                Mike HankeyM Offline
                Mike Hankey
                wrote on last edited by
                #7

                The only thing I can think of is that he didn't know that both were the same and just wanted to keep a separation of concern?

                New version: WinHeist Version 2.1.1 new web site. I know the voices in my head are not real but damn they come up with some good ideas!

                1 Reply Last reply
                0
                • U User 11360346

                  We can't predict looking the above code since we don't know what Dosomething() method exactly do. What we can infer is that there is a socket processing behind since there is a catch exception on it.

                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #8

                  It's irrelevant what DoSomething() does in regard to the catch-blocks. In case of an exception, either the first or the second catch block gets executed and they do exactly the same, so the more specific one is superfluous.

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  U 1 Reply Last reply
                  0
                  • S Sascha Lefevre

                    It's irrelevant what DoSomething() does in regard to the catch-blocks. In case of an exception, either the first or the second catch block gets executed and they do exactly the same, so the more specific one is superfluous.

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    U Offline
                    U Offline
                    User 11360346
                    wrote on last edited by
                    #9

                    They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.

                    OriginalGriffO S V 3 Replies Last reply
                    0
                    • J jeeves77

                      Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

                      try
                      {
                      DoSomething();
                      }
                      catch (SocketException e)
                      {
                      Console.WriteLine(e.Message);
                      conn.connecting = false;
                      }
                      catch (Exception e)
                      {
                      Console.WriteLine(e.Message);
                      conn.connecting = false;
                      }

                      What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

                      J Offline
                      J Offline
                      Jacquers
                      wrote on last edited by
                      #10

                      Typically you would want to handle a socket exception different than a general exception. Maybe an oversight in this case that both are handled the same way. Might have been put in there for debugging.

                      1 Reply Last reply
                      0
                      • U User 11360346

                        They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.

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

                        It doesn't matter. Regardless of what exception is thrown, the code executed will be identical. Which means that the separate catch for the "lesser" exception need not be there at all. And if it needed be there, why put it in? All that does is leave a "hole" where duplicated code can become different over time and cause other problems - that's one of the advantages of inheritance: it removes the need to "copy and paste" code by reusing a single method in all derived classes. So if the method needs to be changed, it's in one single place instead of scattered all over the file and prone to being missed when the updates are done.

                        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

                        U 1 Reply Last reply
                        0
                        • U User 11360346

                          They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.

                          S Offline
                          S Offline
                          Sascha Lefevre
                          wrote on last edited by
                          #12

                          I assume we're talking past each other. The question isn't if catch blocks catching different types of exceptions can be useful but if it's of any use in this concrete case - which it isn't because the exceptions aren't handled differently.

                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            It doesn't matter. Regardless of what exception is thrown, the code executed will be identical. Which means that the separate catch for the "lesser" exception need not be there at all. And if it needed be there, why put it in? All that does is leave a "hole" where duplicated code can become different over time and cause other problems - that's one of the advantages of inheritance: it removes the need to "copy and paste" code by reusing a single method in all derived classes. So if the method needs to be changed, it's in one single place instead of scattered all over the file and prone to being missed when the updates are done.

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

                            U Offline
                            U Offline
                            User 11360346
                            wrote on last edited by
                            #13

                            We all know that managed .Net exception are derived from Exception class. If you are asking why the rest of derived Exception such as socketexception, Dbexception are created then that is another question. And it won't be necessary to allow multiple catch statement block from the compiler. Off course if you leave/comment out the above socketexception then the error definitely go to exception block, b/c of the reason that I stated above. Onething sure to know here, you can't determine to refactor the code by looking that code only, even we don't know either a SocketException will occur or not. G. Day

                            OriginalGriffO 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              There is a recommended practice where you catch the minimum exception you can: SocketException instead of Exception for example, but that code's silly. It's possible that he planned to come back and do something different for the SocketException later and never got round to it, but if that's the case the author still needs a thump upside the head... :laugh:

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

                              Z Offline
                              Z Offline
                              ZurdoDev
                              wrote on last edited by
                              #14

                              OriginalGriff wrote:

                              It's possible that he planned to come back and do something different for the SocketException later and never got round to it

                              That was my thought exactly. :thumbsup:

                              There are only 10 types of people in the world, those who understand binary and those who don't.

                              1 Reply Last reply
                              0
                              • J jeeves77

                                Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

                                try
                                {
                                DoSomething();
                                }
                                catch (SocketException e)
                                {
                                Console.WriteLine(e.Message);
                                conn.connecting = false;
                                }
                                catch (Exception e)
                                {
                                Console.WriteLine(e.Message);
                                conn.connecting = false;
                                }

                                What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #15

                                Well, that could have been more interesting, e.g.

                                catch (SocketException e)
                                {
                                Console.WriteLine(e.Message);
                                conn.connecting = false;
                                throw;
                                }
                                catch (Exception e)
                                {
                                Console.WriteLine(e.Message);
                                conn.connecting = false;
                                }

                                :omg:

                                1 Reply Last reply
                                0
                                • U User 11360346

                                  We all know that managed .Net exception are derived from Exception class. If you are asking why the rest of derived Exception such as socketexception, Dbexception are created then that is another question. And it won't be necessary to allow multiple catch statement block from the compiler. Off course if you leave/comment out the above socketexception then the error definitely go to exception block, b/c of the reason that I stated above. Onething sure to know here, you can't determine to refactor the code by looking that code only, even we don't know either a SocketException will occur or not. G. Day

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

                                  It doesn't matter if a SocketException or an AliensLandedOnWhiteHouseLawnException occurs with that code: the code that is executed is identical regardless. That is the point. Not that Socket Exceptions are derived from Exception - we all know that - but that having a separate catch block is silly if the code executed is the same anyway! Look at the code. Assume a SocketException occurs in the method. What lines of code are executed? Now assume that a AliensLandedOnWhiteHouseLawnException occurs instead. What lines of code are executed? :laugh:

                                  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

                                  U 1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    It doesn't matter if a SocketException or an AliensLandedOnWhiteHouseLawnException occurs with that code: the code that is executed is identical regardless. That is the point. Not that Socket Exceptions are derived from Exception - we all know that - but that having a separate catch block is silly if the code executed is the same anyway! Look at the code. Assume a SocketException occurs in the method. What lines of code are executed? Now assume that a AliensLandedOnWhiteHouseLawnException occurs instead. What lines of code are executed? :laugh:

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

                                    U Offline
                                    U Offline
                                    User 11360346
                                    wrote on last edited by
                                    #17

                                    It does matter, a self ref [^] How do you know the code executed the same ? YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO. What we know both exception was not utilized as such.

                                    OriginalGriffO 1 Reply Last reply
                                    0
                                    • U User 11360346

                                      It does matter, a self ref [^] How do you know the code executed the same ? YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO. What we know both exception was not utilized as such.

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

                                      Member 11394652 wrote:

                                      YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO

                                      Seriously, it doesn't matter. Look at the code we do have:

                                      catch (SocketException e)
                                      {
                                      Console.WriteLine(e.Message);
                                      conn.connecting = false;
                                      }
                                      catch (Exception e)
                                      {
                                      Console.WriteLine(e.Message);
                                      conn.connecting = false;
                                      }

                                      The code in each catch block is identical. :laugh:

                                      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

                                      U 1 Reply Last reply
                                      0
                                      • J jeeves77

                                        Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...

                                        try
                                        {
                                        DoSomething();
                                        }
                                        catch (SocketException e)
                                        {
                                        Console.WriteLine(e.Message);
                                        conn.connecting = false;
                                        }
                                        catch (Exception e)
                                        {
                                        Console.WriteLine(e.Message);
                                        conn.connecting = false;
                                        }

                                        What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)

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

                                        Just useful while debugging; you can have a breakpoint in one block but not the other; but it should be removed afterward.

                                        1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          Member 11394652 wrote:

                                          YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO

                                          Seriously, it doesn't matter. Look at the code we do have:

                                          catch (SocketException e)
                                          {
                                          Console.WriteLine(e.Message);
                                          conn.connecting = false;
                                          }
                                          catch (Exception e)
                                          {
                                          Console.WriteLine(e.Message);
                                          conn.connecting = false;
                                          }

                                          The code in each catch block is identical. :laugh:

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

                                          U Offline
                                          U Offline
                                          User 11360346
                                          wrote on last edited by
                                          #20

                                          Are you saying the message will always be the same ? This is a trick, If you do get it. :)

                                          OriginalGriffO 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