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. Some C# code that makes me sick...

Some C# code that makes me sick...

Scheduled Pinned Locked Moved The Lounge
csharpquestioncomhelp
46 Posts 29 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.
  • S Super Lloyd

    Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

    try {
    DoX();
    }
    catch (Exception ex) {
    throw new Exception("Having problem Doing X!", ex);
    }

    And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

    Ugh, I've seen code like that many times!

    try {
    DoX();
    }
    catch (Exception ex) {
    throw new Exception("Having problem Doing X.", ex);
    }

    Fixed it. That exclamation mark is just screamy and unprofessional :rolleyes:

    Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

    S M 2 Replies Last reply
    0
    • S Super Lloyd

      Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

      try {
      DoX();
      }
      catch (Exception ex) {
      throw new Exception("Having problem Doing X!", ex);
      }

      And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      D Offline
      D Offline
      Dean Allcorn
      wrote on last edited by
      #4

      ditching original exception seems like a good idea (no)

      1 Reply Last reply
      0
      • H honey the codewitch

        I agree that it should probably be something more like:

        // TODO: Log an error
        try {
        DoX();
        }
        catch {
        throw;
        }

        because that will preserve the error, while still putting in an opportunity to add logging, and kind of a placeholder in the code. Avoiding catching the exception type not only makes explicit that you don't care what kind of exception it is, it also forces you to define your exceptions once you add logging. After all you can't log an exception if you don't capture it in a type, so when you do get around to logging it forces you to consider your exception types at that point, leading to at least a half step toward solving your second complaint about throwing a zillion errors, because you're more selective about what you throw. However, I'd argue the above is probably not worth it, but I think that comes down to a matter of style. I certainly wouldn't balk at the above code, but I probably wouldn't write it myself.

        Real programmers use butterflies

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #5

        Because you haven't imagined the horror to its full extent yet!!!

        void Method_1()
        {
        try {
        Method_2();
        } catch (Exception ex) {
        // logging maybe?
        throw new Exception("Method_2", ex);
        }
        }
        void Method_2()
        {
        try {
        Method_3();
        } catch (Exception ex) {
        // logging?
        throw new Exception("Method_3", ex);
        }
        }
        void Method_3()
        {
        try {
        Something();
        } catch (Exception ex) {
        // logging again?
        throw new Exception("Something", ex);
        }
        }

        I'd say how about that?

        void Method_1()
        {
        Method_2();
        }
        void Method_2()
        {
        Method_3();
        }
        void Method_3()
        {
        Something();
        }

        or that

        void Method_1()
        {
        try {
        Method_2();
        } catch (Exception ex) {
        // logging perhaps?
        throw new Exception("Method_2", ex);
        }
        }
        void Method_2()
        {
        Method_3();
        }
        void Method_3()
        {
        Something();
        }

        instead!

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

          Ugh, I've seen code like that many times!

          try {
          DoX();
          }
          catch (Exception ex) {
          throw new Exception("Having problem Doing X.", ex);
          }

          Fixed it. That exclamation mark is just screamy and unprofessional :rolleyes:

          Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #6

          Glad you understand! :laugh:

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          1 Reply Last reply
          0
          • S Super Lloyd

            Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

            try {
            DoX();
            }
            catch (Exception ex) {
            throw new Exception("Having problem Doing X!", ex);
            }

            And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

            <JOKE> Well, obviously it should be more like:

            try
            {
            DoX();
            }
            catch (Exception ex)
            {
            throw ex;
            }

            ;P </JOKE> Personally, I wish the Exception class had been declared abstract. If you need to throw a new exception, it should always be something more specific than the base Exception class. Especially prior to .NET 4 and the HandleProcessCorruptedStateExceptions attribute[^] , when a catch block for the base Exception class would also catch "corrupted process state" exceptions, which should pretty much never be caught.


            "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

            S 1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              Ugh, I've seen code like that many times!

              try {
              DoX();
              }
              catch (Exception ex) {
              throw new Exception("Having problem Doing X.", ex);
              }

              Fixed it. That exclamation mark is just screamy and unprofessional :rolleyes:

              Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

              M Offline
              M Offline
              Member_14885955
              wrote on last edited by
              #8

              I always think that putting an exclamation mark at the end of an error message is the equivalent of adding "you idiot!". "Value must be numeric, you idiot!" "Date must be in d-ddd:mmm/yyyyy format, you idiot!" "Credit card number must not include spaces, you idiot!" (world's most annoying message: why can't you remove the spaces yourself?) Or in "success" messages: "File has been saved!" (like it's some sort of special achievement, and the computer is being extra kind to you in doing it)

              D Sander RosselS 2 Replies Last reply
              0
              • S Super Lloyd

                Because you haven't imagined the horror to its full extent yet!!!

                void Method_1()
                {
                try {
                Method_2();
                } catch (Exception ex) {
                // logging maybe?
                throw new Exception("Method_2", ex);
                }
                }
                void Method_2()
                {
                try {
                Method_3();
                } catch (Exception ex) {
                // logging?
                throw new Exception("Method_3", ex);
                }
                }
                void Method_3()
                {
                try {
                Something();
                } catch (Exception ex) {
                // logging again?
                throw new Exception("Something", ex);
                }
                }

                I'd say how about that?

                void Method_1()
                {
                Method_2();
                }
                void Method_2()
                {
                Method_3();
                }
                void Method_3()
                {
                Something();
                }

                or that

                void Method_1()
                {
                try {
                Method_2();
                } catch (Exception ex) {
                // logging perhaps?
                throw new Exception("Method_2", ex);
                }
                }
                void Method_2()
                {
                Method_3();
                }
                void Method_3()
                {
                Something();
                }

                instead!

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                R Offline
                R Offline
                Rage
                wrote on last edited by
                #9

                I think this deserves a post in the Weird and the Wonderful forum-

                Do not escape reality : improve reality !

                S 1 Reply Last reply
                0
                • R Rage

                  I think this deserves a post in the Weird and the Wonderful forum-

                  Do not escape reality : improve reality !

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #10

                  Yea, I realized that afterwards! ^_^

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    <JOKE> Well, obviously it should be more like:

                    try
                    {
                    DoX();
                    }
                    catch (Exception ex)
                    {
                    throw ex;
                    }

                    ;P </JOKE> Personally, I wish the Exception class had been declared abstract. If you need to throw a new exception, it should always be something more specific than the base Exception class. Especially prior to .NET 4 and the HandleProcessCorruptedStateExceptions attribute[^] , when a catch block for the base Exception class would also catch "corrupted process state" exceptions, which should pretty much never be caught.


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

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #11

                    another tragic mistake! You should have just

                    throw;

                    ;P That preserve the calllstack ^_^ Hey, I didn't know this attribute! :O

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    Richard DeemingR 1 Reply Last reply
                    0
                    • S Super Lloyd

                      another tragic mistake! You should have just

                      throw;

                      ;P That preserve the calllstack ^_^ Hey, I didn't know this attribute! :O

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

                      That was my whole point - I've lost track of the number of times I've seen catch (Exception ex) { throw ex; } used in QA questions. :)


                      "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

                      S 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        That was my whole point - I've lost track of the number of times I've seen catch (Exception ex) { throw ex; } used in QA questions. :)


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

                        S Offline
                        S Offline
                        Super Lloyd
                        wrote on last edited by
                        #13

                        Haha, got me! :)

                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                        1 Reply Last reply
                        0
                        • M Member_14885955

                          I always think that putting an exclamation mark at the end of an error message is the equivalent of adding "you idiot!". "Value must be numeric, you idiot!" "Date must be in d-ddd:mmm/yyyyy format, you idiot!" "Credit card number must not include spaces, you idiot!" (world's most annoying message: why can't you remove the spaces yourself?) Or in "success" messages: "File has been saved!" (like it's some sort of special achievement, and the computer is being extra kind to you in doing it)

                          D Offline
                          D Offline
                          dandy72
                          wrote on last edited by
                          #14

                          Member 14885955 wrote:

                          "File has been saved!" (like it's some sort of special achievement, and the computer is being extra kind to you in doing it)

                          Maybe the software is so bad that successfully saving a file is the exceptional case...

                          1 Reply Last reply
                          0
                          • S Super Lloyd

                            Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                            try {
                            DoX();
                            }
                            catch (Exception ex) {
                            throw new Exception("Having problem Doing X!", ex);
                            }

                            And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                            D Offline
                            D Offline
                            dandy72
                            wrote on last edited by
                            #15

                            While the message reported here is rather useless, I could see it containing useful details, and the expectation is that somebody among the callers will log it.

                            1 Reply Last reply
                            0
                            • S Super Lloyd

                              Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                              try {
                              DoX();
                              }
                              catch (Exception ex) {
                              throw new Exception("Having problem Doing X!", ex);
                              }

                              And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                              M Offline
                              M Offline
                              MSBassSinger
                              wrote on last edited by
                              #16

                              It would be better to add info at each step up the call stack (for debugging info) into the exception's Data collection, then just a throw; statement. Once at the top of the call stack, handle the exception and log the contents of the exception and its data collection.

                              1 Reply Last reply
                              0
                              • S Super Lloyd

                                Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                                try {
                                DoX();
                                }
                                catch (Exception ex) {
                                throw new Exception("Having problem Doing X!", ex);
                                }

                                And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                S Offline
                                S Offline
                                Slacker007
                                wrote on last edited by
                                #17

                                try catch statements should only be used in the UI/client layer, not in business layer or data layer - usually. you should ALWAYS log your exceptions to a database or file log without exception. you should not use try/catch/exceptions to control logic flow, whenever possible. my 2 cents. the code you referenced is crap IMHO. instead of re-throwing the exception, it should be logged, and handled gracefully and informatively for the end user.

                                N 1 Reply Last reply
                                0
                                • M Member_14885955

                                  I always think that putting an exclamation mark at the end of an error message is the equivalent of adding "you idiot!". "Value must be numeric, you idiot!" "Date must be in d-ddd:mmm/yyyyy format, you idiot!" "Credit card number must not include spaces, you idiot!" (world's most annoying message: why can't you remove the spaces yourself?) Or in "success" messages: "File has been saved!" (like it's some sort of special achievement, and the computer is being extra kind to you in doing it)

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

                                  I actually know someone who puts an exclamation behind pretty much every message box! Or three in case of an error!!! Really childish and unprofessional and this is a smart guy with good business instincts (but no programming instincts whatsoever :laugh: ).

                                  Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                                  1 Reply Last reply
                                  0
                                  • S Super Lloyd

                                    Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                                    try {
                                    DoX();
                                    }
                                    catch (Exception ex) {
                                    throw new Exception("Having problem Doing X!", ex);
                                    }

                                    And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

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

                                    It's perfectly legal. There's nothing wrong with that. I do that all the time.

                                    1 Reply Last reply
                                    0
                                    • S Super Lloyd

                                      Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                                      try {
                                      DoX();
                                      }
                                      catch (Exception ex) {
                                      throw new Exception("Having problem Doing X!", ex);
                                      }

                                      And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                      P Offline
                                      P Offline
                                      Peter Adam
                                      wrote on last edited by
                                      #20

                                      "And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk..." That's the Java Spring way, and 128 other exceptions ...

                                      1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                                        try {
                                        DoX();
                                        }
                                        catch (Exception ex) {
                                        throw new Exception("Having problem Doing X!", ex);
                                        }

                                        And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                        U Offline
                                        U Offline
                                        User 12996721
                                        wrote on last edited by
                                        #21

                                        The code I support came with hundreds of

                                        try
                                        {
                                        ... do stuff ...
                                        }
                                        catch (Exception ex)
                                        {
                                        throw ex;
                                        }

                                        I spoke to the original developer, but they are still doing the same thing even now.

                                        S 1 Reply Last reply
                                        0
                                        • S Super Lloyd

                                          Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!

                                          try {
                                          DoX();
                                          }
                                          catch (Exception ex) {
                                          throw new Exception("Having problem Doing X!", ex);
                                          }

                                          And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!

                                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                          G Offline
                                          G Offline
                                          grralph1
                                          wrote on last edited by
                                          #22

                                          Agree. I did like Sanders fix though. However the sub or function that they are calling is defined as a despicable act. Maybe you should have complained about that as well. :)

                                          "Rock journalism is people who can't write interviewing people who can't talk for people who can't read." Frank Zappa 1980

                                          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