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. Unnecessary try / catch block

Unnecessary try / catch block

Scheduled Pinned Locked Moved The Weird and The Wonderful
algorithmsquestion
8 Posts 6 Posters 9 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.
  • D Offline
    D Offline
    David Rush
    wrote on last edited by
    #1

    Whilre reviewing the code from some contract developers, I have found a common construct: public void SomeMethod() { try { // some code here... } catch (Exception exception) { throw exception; // Just re-throw } } Why just catch and re-throw an exception? This just adds to the complexity of the execution without doing anything in return.

    P G B 3 Replies Last reply
    0
    • D David Rush

      Whilre reviewing the code from some contract developers, I have found a common construct: public void SomeMethod() { try { // some code here... } catch (Exception exception) { throw exception; // Just re-throw } } Why just catch and re-throw an exception? This just adds to the complexity of the execution without doing anything in return.

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

      I would wrap that in an # if DEBUG; too many times when debugging I can't figure out where an exception happened, especially when there's a finally involved.

      D E D 3 Replies Last reply
      0
      • P PIEBALDconsult

        I would wrap that in an # if DEBUG; too many times when debugging I can't figure out where an exception happened, especially when there's a finally involved.

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

        Yep it can be useful for debugging. Anyway, I'd use just throw; to rethrow exception, as that way I don't lose stack trace IIRC.


        "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

        1 Reply Last reply
        0
        • P PIEBALDconsult

          I would wrap that in an # if DEBUG; too many times when debugging I can't figure out where an exception happened, especially when there's a finally involved.

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          You know you can get VS to break on exceptions when debugging so even if the code is wrapped in a try - catch / finally block it'll always break when the exception is thrown rather than when it's un-handled.

          1 Reply Last reply
          0
          • D David Rush

            Whilre reviewing the code from some contract developers, I have found a common construct: public void SomeMethod() { try { // some code here... } catch (Exception exception) { throw exception; // Just re-throw } } Why just catch and re-throw an exception? This just adds to the complexity of the execution without doing anything in return.

            G Offline
            G Offline
            GWB s1c
            wrote on last edited by
            #5

            I once worked with a programmer who wrote code like that. He yelled at me (in front of the big boss) for getting rid of it. It turns out he wasn't aware that .Net puts a stack trace in Exception objects! The real lesson that *I* learned is "old habits die hard"; he was still using techniques from back in the C++ / COM days.

            D 1 Reply Last reply
            0
            • P PIEBALDconsult

              I would wrap that in an # if DEBUG; too many times when debugging I can't figure out where an exception happened, especially when there's a finally involved.

              D Offline
              D Offline
              David Rush
              wrote on last edited by
              #6

              I agree with the use of conditional statements for this purpose, but they are not there and this is supposedly debugged code. I can also see re-throwing if there is a finally block cleaning up, regardless of the use of conditionals. What I didn't show was that there are typically three or four lines of code inside the try block...

              1 Reply Last reply
              0
              • G GWB s1c

                I once worked with a programmer who wrote code like that. He yelled at me (in front of the big boss) for getting rid of it. It turns out he wasn't aware that .Net puts a stack trace in Exception objects! The real lesson that *I* learned is "old habits die hard"; he was still using techniques from back in the C++ / COM days.

                D Offline
                D Offline
                David Rush
                wrote on last edited by
                #7

                I feel you pain. Old habits do die hard, so the trick is learning the right habbits. Asking yourself "Do we have a backup?" is the best habbit to have. Beyond that is a functional understanding of what the compiler is doing to your source, regardless of the language. Unfortunately too many people try to compare a medium-level language like C or C++ to a high level language like C# based on the syntactical similarities alone.

                1 Reply Last reply
                0
                • D David Rush

                  Whilre reviewing the code from some contract developers, I have found a common construct: public void SomeMethod() { try { // some code here... } catch (Exception exception) { throw exception; // Just re-throw } } Why just catch and re-throw an exception? This just adds to the complexity of the execution without doing anything in return.

                  B Offline
                  B Offline
                  Brian R
                  wrote on last edited by
                  #8

                  Actually, this code has a somewhat subtle behavior, since it is catching by-value, prior to throwing. If a derived exception is thrown, it will be "clipped" to the base class when it is thrown. This could be potentially different from invoking throw, which would throw the derived class.

                  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