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. try-catch code convention [modified]

try-catch code convention [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
career
30 Posts 21 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.
  • K Kevin Drzycimski

    a fellow student told me of a strange code convention in his job: almost every single method looks like this

    //method head
    {
    try {
    //actual method body
    }
    catch (...) {
    }
    }

    directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

    modified on Thursday, July 1, 2010 2:57 PM

    R Offline
    R Offline
    RugbyLeague
    wrote on last edited by
    #2

    Discarding errors is a great way to give the user the perception that all is right with the world.

    S L 2 Replies Last reply
    0
    • R RugbyLeague

      Discarding errors is a great way to give the user the perception that all is right with the world.

      S Offline
      S Offline
      supercat9
      wrote on last edited by
      #3

      RugbyLeague wrote:

      Discarding errors is a great way to give the user the perception that all is right with the world.

      Old VB6 provided a more convenient means, though: "ON ERROR RESUME NEXT". Unfortunately, its lack of "Try"-style methods for many things(*) tended to make a more appropriate coding style painful. (*) There are many places where one might want to do an operation which one expects might fail, and not worry if it does; e.g. one may want to create a table if it doesn't exist, but ignore it if it does. Checking for existence before creation won't completely solve the problem, since another client might create the table after the existence check. The nicest way would be to say "do command X, but don't worry if it fails." Unfortunately, the only convenient way to do that is with ON ERROR RESUME NEXT, a concept so ugly Microsoft defined a special statement just for it. Incidentally, if a routine does an ON ERROR RESUME NEXT and then invokes another routine that fails but does not have an ON ERROR statement, that other routine will exit out to the routine which did the ON ERROR RESUME NEXT. Nasty, but not so horrible as ON ERROR RESUME, which would restart the entire statement in the parent routine, likely causing many statements in the called routine to be re-executed.

      1 Reply Last reply
      0
      • K Kevin Drzycimski

        a fellow student told me of a strange code convention in his job: almost every single method looks like this

        //method head
        {
        try {
        //actual method body
        }
        catch (...) {
        }
        }

        directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

        modified on Thursday, July 1, 2010 2:57 PM

        J Offline
        J Offline
        Jon Sagara
        wrote on last edited by
        #4

        Kevin Drzycimski wrote:

        directly after the head opens a try block over the whole body. all errors are just thrown away.

        In Enterprise lingo, that's called the Try/Swallow pattern.

        Jon Sagara Some see the glass as half-empty, some see the glass as half-full. I see the glass as too big. -- George Carlin .NET Blog | Personal Blog | Articles

        D 1 Reply Last reply
        0
        • K Kevin Drzycimski

          a fellow student told me of a strange code convention in his job: almost every single method looks like this

          //method head
          {
          try {
          //actual method body
          }
          catch (...) {
          }
          }

          directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

          modified on Thursday, July 1, 2010 2:57 PM

          J Offline
          J Offline
          Jeroen De Dauw
          wrote on last edited by
          #5

          So, did they patent this? o_O

          Jeroen De Dauw
          Blog ; Wiki

          K 1 Reply Last reply
          0
          • J Jeroen De Dauw

            So, did they patent this? o_O

            Jeroen De Dauw
            Blog ; Wiki

            K Offline
            K Offline
            Kevin Drzycimski
            wrote on last edited by
            #6

            dont know, but I had to laugh so hard when he told me this... everybody who knows the disciplines of "Software Engineering" must either laugh or puke

            A 1 Reply Last reply
            0
            • K Kevin Drzycimski

              dont know, but I had to laugh so hard when he told me this... everybody who knows the disciplines of "Software Engineering" must either laugh or puke

              A Offline
              A Offline
              accesstomypc
              wrote on last edited by
              #7

              It's from BP's oil valve device driver source code..

              M 1 Reply Last reply
              0
              • A accesstomypc

                It's from BP's oil valve device driver source code..

                M Offline
                M Offline
                Matt U
                wrote on last edited by
                #8

                Haha, funny. Sadly enough, I used to swallow exceptions in code as well. However, I'm not in the professional field, just personal. It worries me that this sort of thing is done by professionally employed developers. xD

                _ 1 Reply Last reply
                0
                • M Matt U

                  Haha, funny. Sadly enough, I used to swallow exceptions in code as well. However, I'm not in the professional field, just personal. It worries me that this sort of thing is done by professionally employed developers. xD

                  _ Offline
                  _ Offline
                  _beauw_
                  wrote on last edited by
                  #9

                  Squelching exceptions can be a legitimate tactic. For example, suppose you're writing an error handling routine. The routine probably tries to do many things related to error handling: write out a log file, send an e-mail, etc. If any of these fails, you won't want this second failure to disrupt the rest of the error routine, nor will you want it to throw a new exception (which would quite likely propagate out to the OS level, and result in a GPF). Another legitimate reason to squelch is the existence of OS level bugs and failures that are outside the developer's control. There was a bug in Windows a few years that caused "insufficient memory" errors whenever the system entered "sleep" mode due to inactivity. I cannot really detect that as a .NET developer, except in the form of an "out of memory" exception. And the error condition is bogus, or at least temporary; so I do not want my program to GPF, I really do want it to ignore the exception and continue trying to operate. Squelching this error is legitimate, I think. I have had architect types tell me that exceptions should never be squelched. That simply doesn't square with reality for me, though. A better policy, I think, is to have guidelines for squelching exceptions, e.g. squelch only the most specific exception type possible, always put an explanatory comment in the "catch" block, don't use this tactic for simple validation of inputs, etc.

                  modified on Saturday, July 3, 2010 12:33 PM

                  J 1 Reply Last reply
                  0
                  • K Kevin Drzycimski

                    a fellow student told me of a strange code convention in his job: almost every single method looks like this

                    //method head
                    {
                    try {
                    //actual method body
                    }
                    catch (...) {
                    }
                    }

                    directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

                    modified on Thursday, July 1, 2010 2:57 PM

                    W Offline
                    W Offline
                    whatrevolution
                    wrote on last edited by
                    #10

                    Apparently you are not a man of faith, and do not realize that this is a Marine software design tactic. Swallow 'em all and let God sort 'em out!

                    Honestly Illustrated

                    <Pretentious> Raid tha manyuhl. :E <Pretentious> Aw raid eh own mah meaxbile. :E

                    1 Reply Last reply
                    0
                    • R RugbyLeague

                      Discarding errors is a great way to give the user the perception that all is right with the world.

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

                      RugbyLeague wrote:

                      a great way to give the user the perception that all is right with the world.

                      That's the goal, according to some of the teachers here. If you find yourself in such a group, by all means, adapt or leave - you'll be the source of all bugs and errors if you try to move to structured error-handling :)

                      I are Troll :suss:

                      S K 2 Replies Last reply
                      0
                      • K Kevin Drzycimski

                        a fellow student told me of a strange code convention in his job: almost every single method looks like this

                        //method head
                        {
                        try {
                        //actual method body
                        }
                        catch (...) {
                        }
                        }

                        directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

                        modified on Thursday, July 1, 2010 2:57 PM

                        N Offline
                        N Offline
                        Nagy Vilmos
                        wrote on last edited by
                        #12

                        I'm guessing that the convention dates from the VB style, used to use this:

                        Function Wibble() As String
                        Dim sResult As String

                        On Local Error GoTo Fail

                        'stuff goes here
                        

                        done:
                        Wibble = sResult
                        Exit Function

                        Fail:
                        LogError "Wibble", Err
                        Resume done
                        End Function


                        Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                        K 1 Reply Last reply
                        0
                        • N Nagy Vilmos

                          I'm guessing that the convention dates from the VB style, used to use this:

                          Function Wibble() As String
                          Dim sResult As String

                          On Local Error GoTo Fail

                          'stuff goes here
                          

                          done:
                          Wibble = sResult
                          Exit Function

                          Fail:
                          LogError "Wibble", Err
                          Resume done
                          End Function


                          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

                          K Offline
                          K Offline
                          Kevin Drzycimski
                          wrote on last edited by
                          #13

                          well, the difference is, that your code logs the error. in try/swallow it just thrown away. logging all the error messages might bloat the logfile ;)

                          1 Reply Last reply
                          0
                          • K Kevin Drzycimski

                            a fellow student told me of a strange code convention in his job: almost every single method looks like this

                            //method head
                            {
                            try {
                            //actual method body
                            }
                            catch (...) {
                            }
                            }

                            directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

                            modified on Thursday, July 1, 2010 2:57 PM

                            B Offline
                            B Offline
                            Bigdeak
                            wrote on last edited by
                            #14

                            If you have common functions reachable by the GUI, that are not important for the main of the application, this is a really usefull and stable act, and i do it in almost the same manner for at least the common methods. For example i wan't to change a image of a specified object. Now my ChangeImage() method throws a unhandled exception, with this try catch block around the method body there will be no quaint exception message, the application won't crash because of a single method exception, only the ChangeImage() method isn't working. ;) Sorry for my bad english.

                            _ 1 Reply Last reply
                            0
                            • _ _beauw_

                              Squelching exceptions can be a legitimate tactic. For example, suppose you're writing an error handling routine. The routine probably tries to do many things related to error handling: write out a log file, send an e-mail, etc. If any of these fails, you won't want this second failure to disrupt the rest of the error routine, nor will you want it to throw a new exception (which would quite likely propagate out to the OS level, and result in a GPF). Another legitimate reason to squelch is the existence of OS level bugs and failures that are outside the developer's control. There was a bug in Windows a few years that caused "insufficient memory" errors whenever the system entered "sleep" mode due to inactivity. I cannot really detect that as a .NET developer, except in the form of an "out of memory" exception. And the error condition is bogus, or at least temporary; so I do not want my program to GPF, I really do want it to ignore the exception and continue trying to operate. Squelching this error is legitimate, I think. I have had architect types tell me that exceptions should never be squelched. That simply doesn't square with reality for me, though. A better policy, I think, is to have guidelines for squelching exceptions, e.g. squelch only the most specific exception type possible, always put an explanatory comment in the "catch" block, don't use this tactic for simple validation of inputs, etc.

                              modified on Saturday, July 3, 2010 12:33 PM

                              J Offline
                              J Offline
                              Jammer 0
                              wrote on last edited by
                              #15

                              I think the point was that it was in EVERY method ...

                              Jammer My Blog | Articles | DMon | SampleSort

                              _ 1 Reply Last reply
                              0
                              • J Jammer 0

                                I think the point was that it was in EVERY method ...

                                Jammer My Blog | Articles | DMon | SampleSort

                                _ Offline
                                _ Offline
                                _beauw_
                                wrote on last edited by
                                #16

                                Originally I think that was the point. The post I actually responded to seemed to impugn any and all squelching of exceptions ("[s]adly enough, I used to swallow exceptions in code as well..."). This blanket statement is actually one I hear a lot, even from people pretty well-placed in software development.

                                1 Reply Last reply
                                0
                                • J Jon Sagara

                                  Kevin Drzycimski wrote:

                                  directly after the head opens a try block over the whole body. all errors are just thrown away.

                                  In Enterprise lingo, that's called the Try/Swallow pattern.

                                  Jon Sagara Some see the glass as half-empty, some see the glass as half-full. I see the glass as too big. -- George Carlin .NET Blog | Personal Blog | Articles

                                  D Offline
                                  D Offline
                                  DaveyM69
                                  wrote on last edited by
                                  #17

                                  Jon Sagara wrote:

                                  Try/Swallow

                                  I'm scared to reply to that one!

                                  Dave

                                  If this helped, please vote & accept answer!

                                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                                  Y 1 Reply Last reply
                                  0
                                  • B Bigdeak

                                    If you have common functions reachable by the GUI, that are not important for the main of the application, this is a really usefull and stable act, and i do it in almost the same manner for at least the common methods. For example i wan't to change a image of a specified object. Now my ChangeImage() method throws a unhandled exception, with this try catch block around the method body there will be no quaint exception message, the application won't crash because of a single method exception, only the ChangeImage() method isn't working. ;) Sorry for my bad english.

                                    _ Offline
                                    _ Offline
                                    _beauw_
                                    wrote on last edited by
                                    #18

                                    I think it's true that failures related to some less significant features should not be allowed to disrupt other features. Even logging these failures in a production environment can impact other critical systems. So, there are probably legitimate cases where errors related to an entire subsystem should be squelched in the production build. Doing this is, at some level, a revival of ON ERROR RESUME NEXT. At a minimum, I think that good programming practices dictate that unhandled exceptions should halt the program in its development builds (even if some of these are squelched in the "release" build). So long as the transition between build modes is automatic and reliable, this can be a good approach to exception handling for some applications.

                                    L 1 Reply Last reply
                                    0
                                    • K Kevin Drzycimski

                                      a fellow student told me of a strange code convention in his job: almost every single method looks like this

                                      //method head
                                      {
                                      try {
                                      //actual method body
                                      }
                                      catch (...) {
                                      }
                                      }

                                      directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

                                      modified on Thursday, July 1, 2010 2:57 PM

                                      K Offline
                                      K Offline
                                      Kunal Chowdhury IN
                                      wrote on last edited by
                                      #19

                                      try{} catch{} blocks should be use only when you need it. Else there will be a performance issue. It is not recommended to write try catch block for the whole code of any method. Where you expect to be a codeflaw, where exception may come just put your try catch there. Don't try to use generic exception in all the case. If you know some specific exception that may come, only catch those explicitly.

                                      Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


                                      Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

                                      1 Reply Last reply
                                      0
                                      • K Kevin Drzycimski

                                        a fellow student told me of a strange code convention in his job: almost every single method looks like this

                                        //method head
                                        {
                                        try {
                                        //actual method body
                                        }
                                        catch (...) {
                                        }
                                        }

                                        directly after the head opens a try block over the whole body. all errors are just thrown away. the programs work and never crash! they are world market leader :-D seems this fast and easy developing is quite successfull at all. they are more engineers, so these kludges seem appropriate.

                                        modified on Thursday, July 1, 2010 2:57 PM

                                        F Offline
                                        F Offline
                                        fjdiewornncalwe
                                        wrote on last edited by
                                        #20

                                        What you don't see, can't hurt you... It'll only hurt the guy who has to fix it.

                                        1 Reply Last reply
                                        0
                                        • _ _beauw_

                                          I think it's true that failures related to some less significant features should not be allowed to disrupt other features. Even logging these failures in a production environment can impact other critical systems. So, there are probably legitimate cases where errors related to an entire subsystem should be squelched in the production build. Doing this is, at some level, a revival of ON ERROR RESUME NEXT. At a minimum, I think that good programming practices dictate that unhandled exceptions should halt the program in its development builds (even if some of these are squelched in the "release" build). So long as the transition between build modes is automatic and reliable, this can be a good approach to exception handling for some applications.

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

                                          _beauw_ wrote:

                                          Even logging these failures in a production environment can impact other critical systems.

                                          ..logging doesn't require much resources (a small ringbuffer?), and should not influence the working of dependent systems. Having a global error handler that logs anything that's not expected helps a lot when maintaining the application.

                                          I are Troll :suss:

                                          _ 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