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!

    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
              • 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!

                T Offline
                T Offline
                Thornik
                wrote on last edited by
                #23

                Your code is not the worst sample of "logic"! :)

                try {
                DoX();
                }
                catch (Exception ex) {
                MessageBox.Show("Something happen!");
                }

                THIS code is a poison of modern apps!! NOBODY knows what happen, where happen, just "close app" and say goodbye to all your work. Even monkeys from MS (I remember - monkeys, SELECTED by HR!) do such things. And when you report to MS "you have Error 0x45454582354", they (like imbeciles) advice you to reboot computer. HEY!! It's your program, get off my cookies, cache, operating system and codecs - fix YOUR failures! damn...

                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
                  DumpsterJuice
                  wrote on last edited by
                  #24

                  If you don't trap the error, well... it will just splat on the user screen. Catching, and deciding what to do, log, or show... will help you build new code, because you will easily be able to fix your old code :) Having said all that, the code block frustrates me too. Its a bit ugly, and tedious. A while back they made properties "easier" with all that get / setter code. I would like to see an auto Trap block, or at least a simpler model for it. Keep It Simple, keep it moving.

                  S 1 Reply Last reply
                  0
                  • D DumpsterJuice

                    If you don't trap the error, well... it will just splat on the user screen. Catching, and deciding what to do, log, or show... will help you build new code, because you will easily be able to fix your old code :) Having said all that, the code block frustrates me too. Its a bit ugly, and tedious. A while back they made properties "easier" with all that get / setter code. I would like to see an auto Trap block, or at least a simpler model for it. Keep It Simple, keep it moving.

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

                    Just so you know, the code does throw an error, the catching is just to add the useless information "I am here" and rethrow... So, your comment, as far as I understand, is irrelevant to the situation...

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

                    D 1 Reply Last reply
                    0
                    • U User 12996721

                      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 Offline
                      S Offline
                      Super Lloyd
                      wrote on last edited by
                      #26

                      Oooo... this is doubly terrible :( - useless try/catch - hiding the original stack! :(( :(( :(( :~

                      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!

                        T Offline
                        T Offline
                        Tecfield
                        wrote on last edited by
                        #27

                        I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use Exception.Data to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0

                        Maybe I, Maybe U, can make a change to the world!

                        S M 2 Replies Last reply
                        0
                        • S Super Lloyd

                          Just so you know, the code does throw an error, the catching is just to add the useless information "I am here" and rethrow... So, your comment, as far as I understand, is irrelevant to the situation...

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

                          D Offline
                          D Offline
                          DumpsterJuice
                          wrote on last edited by
                          #28

                          OK I think I see your point now - someone codes a trap and really doesn't do anything with it but comment? Is that it? Keep It Simple, keep it moving.

                          S 1 Reply Last reply
                          0
                          • T Tecfield

                            I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use Exception.Data to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0

                            Maybe I, Maybe U, can make a change to the world!

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

                            Yes, but in that case it's not happening! ;)

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

                            1 Reply Last reply
                            0
                            • D DumpsterJuice

                              OK I think I see your point now - someone codes a trap and really doesn't do anything with it but comment? Is that it? Keep It Simple, keep it moving.

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

                              Indeed.... It goes further, as I explained tersely... This is recursively done in multiple method that catch and rethrow each other, making the code more verbose and slower and less debugable without any benefit...

                              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!

                                B Offline
                                B Offline
                                BernardIE5317
                                wrote on last edited by
                                #31

                                Thank you for your post. It got me thinking. Maybe I should place a try at each method's entry and a catch at each method's exit so as to add the method's signature and argument list to the exception message. A one line try at entry and a one line catch at exit doesn't offend my sense of aesthetics. Unfortunately as far as I know it would require knowing the types of exceptions thrown by each called method and I don't know how to do that w/o it being a pain in the rear. Perhaps this could be automated. I don't see why not. Something to cogitate on. For now though I have some hopefully exceptional code to write. - Cheerio

                                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!

                                  T Offline
                                  T Offline
                                  Tecfield
                                  wrote on last edited by
                                  #32

                                  I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use Exception.Data to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0

                                  Maybe I, Maybe U, can make a change to the world!

                                  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!

                                    A Offline
                                    A Offline
                                    agolddog
                                    wrote on last edited by
                                    #33

                                    If they logged, or added something useful to the new Exception (let's say there were parameters to this method, and the exception method was more like ["Having trouble doing X for " + param.ToString()]), then maybe I could see an excuse for a catch and throw. As you show it, you're right to burn it to the ground.

                                    1 Reply Last reply
                                    0
                                    • S Slacker007

                                      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 Offline
                                      N Offline
                                      NightPen
                                      wrote on last edited by
                                      #34

                                      *clapping* Finally, another dev gets it! If you are not going to take care of the exception then don't catch it. And by taking care of the exception, I don't mean to log it and forget it. Actually, do something about it.

                                      1 Reply Last reply
                                      0
                                      • T Tecfield

                                        I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use Exception.Data to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0

                                        Maybe I, Maybe U, can make a change to the world!

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

                                        More times than I can count, I have seen debugging and fix time for a production issue cut from days to hours, or hours to minutes, because the appropriate runtime variables were added as name-value pairs to the exception's Data collection. That reduced time is gold to a software project during the full SDLC.

                                        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
                                          PeteMcNamee
                                          wrote on last edited by
                                          #36

                                          :omg:

                                          Pete McNamee "True knowledge exists in knowing that you know nothing."

                                          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