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.
  • V Vikram A Punathambekar

    Member 11394652 wrote:

    SocketException sex

    :)

    Cheers, विक्रम "We have already been through this, I am not going to repeat myself." - fat_boy, in a global warming thread :doh:

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

    :)

    1 Reply Last reply
    0
    • U User 11360346

      The fact that you still ignore the Dosomething() method do, you generalize that both catches block get the same exception. Even I gave you example that the method might throw DBException and you expect the SocketException get exception based on your assumption.

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

      I don't need to know what DoSomething() does to reason this: original code: SocketException > catch block 1 > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block 2 > { Console.WriteLine(e.Message); conn.connecting = false; } after removing catch block 1: SocketException > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } ergo: catch block 1 is redundant

      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
      • U User 11360346

        The fact that you still ignore the Dosomething() method do, you generalize that both catches block get the same exception. Even I gave you example that the method might throw DBException and you expect the SocketException get exception based on your assumption.

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

        No, I don't. But both catch blocks have the same code. So they both do the same thing regardless of which exception gets fired.

        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...)

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #27

          Looks like code I would have written except that I would have put a TODO in the SocketException handler with a note to do something more useful than what the generic exception handler does. Or the other way around. Either way, the intention would be to come back later and fix it. Marc

          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

          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...)

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

            I'd lay odds that the original programmer thought he may 'legitimately' get a socketException error, so catches it and handles it appropriately. (though in this case, just logs it) But then wants to catch and log any other exception, just in case. So the fault is in the fact that in the 1st catch he should probably have been putting some relevant code (I dunno - pop up a message to the user or something) as it is an 'expected' exception. As I think Marc said - looks like a TODO is missing!

            PooperPig - Coming Soon

            1 Reply Last reply
            0
            • S Sascha Lefevre

              I don't need to know what DoSomething() does to reason this: original code: SocketException > catch block 1 > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block 2 > { Console.WriteLine(e.Message); conn.connecting = false; } after removing catch block 1: SocketException > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } ergo: catch block 1 is redundant

              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
              #29

              Yes you do need to have the catch block. In fact you may need to add more based on what Dosomething()does. What op need to do is to implement the appropriate logic in the catch block which I already said it in my first response.

              S 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                No, I don't. But both catch blocks have the same code. So they both do the same thing regardless of which exception gets fired.

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

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

                See my reply here. [^]

                1 Reply Last reply
                0
                • U User 11360346

                  Yes you do need to have the catch block. In fact you may need to add more based on what Dosomething()does. What op need to do is to implement the appropriate logic in the catch block which I already said it in my first response.

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

                  Take the code from the original post and put it into Main() of a new console project. Replace conn.connecting with a bool connecting or throw it out. Instead of DoSomething() throw a SocketException or create a method DoSomething() and throw the SocketException there. Run it and take note of the console output. Remove the first catch block and run again. You'll notice that it's the same output. Ergo the first catch block is redundant in this particular code sample.

                  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

                    Take the code from the original post and put it into Main() of a new console project. Replace conn.connecting with a bool connecting or throw it out. Instead of DoSomething() throw a SocketException or create a method DoSomething() and throw the SocketException there. Run it and take note of the console output. Remove the first catch block and run again. You'll notice that it's the same output. Ergo the first catch block is redundant in this particular code sample.

                    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
                    #32

                    No that is not the intension. Op misses to implement the appropriate logic behind once the error occurred in SocketException which he copied similar catch block in the Exception block.

                    S 1 Reply Last reply
                    0
                    • U User 11360346

                      No that is not the intension. Op misses to implement the appropriate logic behind once the error occurred in SocketException which he copied similar catch block in the Exception block.

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

                      True. But you missed that we were explicitly not talking about how it should be done right but about whether the two catch blocks make sense the way they are.

                      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

                        True. But you missed that we were explicitly not talking about how it should be done right but about whether the two catch blocks make sense the way they are.

                        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
                        #34

                        They make sense since the Op knows that there is socket related exception which Op wrote to it's own catch block. To me the the code was only getting SocketException for a while and then he got other excepion which it couldn't caught by SocketException and then he added the same catch code block with Exception without taking the account of future coders since he got relief at that moment.

                        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