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