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. Other Discussions
  3. The Weird and The Wonderful
  4. Empty catches’ blocks

Empty catches’ blocks

Scheduled Pinned Locked Moved The Weird and The Wonderful
helpdatabasedebuggingsalestutorial
23 Posts 14 Posters 17 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 ScottM1

    Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace

    There are 10 types of people in the world, those who understand binary and those who dont.

    N Offline
    N Offline
    NormDroid
    wrote on last edited by
    #4

    True, especially when .net throws exceptions willy nilly.

    .net is a box of never ending treasures, every day I get find another gem.

    1 Reply Last reply
    0
    • S ScottM1

      Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace

      There are 10 types of people in the world, those who understand binary and those who dont.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #5

      If you have one of those, go hunting for an alternate approach that does not throw an exception. In .NET 1.x, it appeared there was no way to convert a string to an integer without getting an exception if it failed. However, you could go via the Double type's TryParse method and then cast the Double to an int. Double cannot represent the whole range of a 64-bit integer so this approach isn't suitable for numbers this large. Thankfully .NET 2.0 added a whole load of TryParse methods for the other basic types. (They do much the same work as the old Double.TryParse).

      Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • S ScottM1

        Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace

        There are 10 types of people in the world, those who understand binary and those who dont.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #6

        Oooohh! The "Damn the torpedoes!" approach to coding, huh? Funny, I just about never write an exception handler that didn't actually handle the exception. If your logic depends on an exception happening, you haven't solved the logic problem properly.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        S 1 Reply Last reply
        0
        • S ScottM1

          Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace

          There are 10 types of people in the world, those who understand binary and those who dont.

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

          I hate you.

          Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Oooohh! The "Damn the torpedoes!" approach to coding, huh? Funny, I just about never write an exception handler that didn't actually handle the exception. If your logic depends on an exception happening, you haven't solved the logic problem properly.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            S Offline
            S Offline
            Sameer Alibhai
            wrote on last edited by
            #8

            FUnny!! The same thing happens at my office all the time, and I even posted an article about it! http://sharpdeveloper.net/content/archive/2007/05/25/do-not-eat-exceptions.aspx Please do read and comment. THank you

            Author, SharpDeveloper.NET (http://www.sharpdeveloper.net)

            D 1 Reply Last reply
            0
            • S Sameer Alibhai

              FUnny!! The same thing happens at my office all the time, and I even posted an article about it! http://sharpdeveloper.net/content/archive/2007/05/25/do-not-eat-exceptions.aspx Please do read and comment. THank you

              Author, SharpDeveloper.NET (http://www.sharpdeveloper.net)

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #9

              Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              S P 2 Replies Last reply
              0
              • R Rage

                I hate you.

                Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

                S Offline
                S Offline
                ScottM1
                wrote on last edited by
                #10

                Look, most of the time it is possible to do things without an exception being thrown but sometimes it is just too much work to prevent it. Therefore catch (Exception){} I mean why bother looking for alternative approaches when try-catching is so easy. -- modified at 2:05 Tuesday 5th June, 2007

                There are 10 types of people in the world, those who understand binary and those who dont.

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  S Offline
                  S Offline
                  ScottM1
                  wrote on last edited by
                  #11

                  Maybe we're missing something, maybe there's more information but the font-color is white. Try selecting everything. :)

                  There are 10 types of people in the world, those who understand binary and those who dont.

                  1 Reply Last reply
                  0
                  • S ScottM1

                    Look, most of the time it is possible to do things without an exception being thrown but sometimes it is just too much work to prevent it. Therefore catch (Exception){} I mean why bother looking for alternative approaches when try-catching is so easy. -- modified at 2:05 Tuesday 5th June, 2007

                    There are 10 types of people in the world, those who understand binary and those who dont.

                    D Offline
                    D Offline
                    DavidNohejl
                    wrote on last edited by
                    #12

                    smyers wrote:

                    I mean why bother looking for alternative approaches when try-catching is so easy.

                    Because it's so damn hard to debug. If you're gonna eat the exception, at least put some logging code in catch block.


                    "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                    S 1 Reply Last reply
                    0
                    • D DavidNohejl

                      smyers wrote:

                      I mean why bother looking for alternative approaches when try-catching is so easy.

                      Because it's so damn hard to debug. If you're gonna eat the exception, at least put some logging code in catch block.


                      "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                      S Offline
                      S Offline
                      ScottM1
                      wrote on last edited by
                      #13

                      dnh wrote:

                      Because it's so damn hard to debug.

                      No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)

                      There are 10 types of people in the world, those who understand binary and those who dont.

                      D P 2 Replies Last reply
                      0
                      • S ScottM1

                        dnh wrote:

                        Because it's so damn hard to debug.

                        No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)

                        There are 10 types of people in the world, those who understand binary and those who dont.

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

                        Then someone start using your code, and spend whole day finiding empty catch buried under tons of code. :sigh: Been there, cursed a lot. :)


                        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                        D 1 Reply Last reply
                        0
                        • D DavidNohejl

                          Then someone start using your code, and spend whole day finiding empty catch buried under tons of code. :sigh: Been there, cursed a lot. :)


                          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                          D Offline
                          D Offline
                          Dan Neely
                          wrote on last edited by
                          #15

                          agreed. If you actually have to eat an exception, make your catch as narrow as possible to let any other exceptions flow up to error reporting systems. You can use string matching off the messagetext to narrow things down even if the base exception is being thrown instead of something more focused.

                          -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                          K 1 Reply Last reply
                          0
                          • S ScottM1

                            dnh wrote:

                            Because it's so damn hard to debug.

                            No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)

                            There are 10 types of people in the world, those who understand binary and those who dont.

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

                            Then put the MessageBox.Show(ex.ToString()); inside conditional code (does VB not have that?).

                            catch ( System.Exception ex )
                            {

                            if DEBUG

                            MessageBox.Show(ex.ToString()); 
                            

                            endif

                            }

                            D 1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Then put the MessageBox.Show(ex.ToString()); inside conditional code (does VB not have that?).

                              catch ( System.Exception ex )
                              {

                              if DEBUG

                              MessageBox.Show(ex.ToString()); 
                              

                              endif

                              }

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #17

                              Yes, it does. The problem is that just eating exceptions willy-nilly is horrifyingly bad practice. It makes someone comming up behind you to maintain your code want to hunt you down and kill you!

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007

                              P 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Yes, it does. The problem is that just eating exceptions willy-nilly is horrifyingly bad practice. It makes someone comming up behind you to maintain your code want to hunt you down and kill you!

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007

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

                                Doing anything willy-nilly tends to lead to problems. All decisions require thought and review.

                                1 Reply Last reply
                                0
                                • D Dave Kreskowiak

                                  Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                       2006, 2007

                                  P Offline
                                  P Offline
                                  Paul Conrad
                                  wrote on last edited by
                                  #19

                                  Dave Kreskowiak wrote:

                                  I've seen more information on a frickin' sticky note!

                                  :laugh::laugh::laugh:

                                  1 Reply Last reply
                                  0
                                  • I Ilya Verbitskiy

                                    Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.

                                    K Offline
                                    K Offline
                                    Kevin McFarlane
                                    wrote on last edited by
                                    #20

                                    Nasty. I've run into problems with these when maintaining code.

                                    Kevin

                                    1 Reply Last reply
                                    0
                                    • S ScottM1

                                      Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace

                                      There are 10 types of people in the world, those who understand binary and those who dont.

                                      K Offline
                                      K Offline
                                      Kevin McFarlane
                                      wrote on last edited by
                                      #21

                                      There might be some scenarios, e.g., retrying. But in that case I would try and narrow down the exception type to an "expected" exception. If you just catch Exception how do you know that you're not swallowing a bug?

                                      Kevin

                                      1 Reply Last reply
                                      0
                                      • D Dan Neely

                                        agreed. If you actually have to eat an exception, make your catch as narrow as possible to let any other exceptions flow up to error reporting systems. You can use string matching off the messagetext to narrow things down even if the base exception is being thrown instead of something more focused.

                                        -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                                        K Offline
                                        K Offline
                                        Kevin McFarlane
                                        wrote on last edited by
                                        #22

                                        dan neely wrote:

                                        If you actually have to eat an exception, make your catch as narrow as possible to let any other exceptions flow up to error reporting systems.

                                        I agree Dan. I've been bitten by bugs swallowed by empty catch blocks that catch Exception. A real pain to debug.

                                        Kevin

                                        1 Reply Last reply
                                        0
                                        • I Ilya Verbitskiy

                                          Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.

                                          F Offline
                                          F Offline
                                          Fabio Zanetta
                                          wrote on last edited by
                                          #23

                                          Yeah, and try something like this:

                                          protected override void OnPaint(PaintEventArgs e)
                                          {
                                          base.OnPaint(e);

                                          try
                                          {
                                              int zero = 0;
                                              int one = 1;
                                              int exception = one / zero;
                                          }
                                          catch
                                          {
                                              throw new InvalidOperationException();
                                          }
                                          

                                          }

                                          Let's say the exception is done by a third part plugin, so you can't control it. Want you try to catch it in the Application.ThreadException event? Ok, try it! ;) Not all scenarios allow you to fill the catch block.


                                          free .net reporting and gdi+ tools www.neodatatype.net

                                          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