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. Catch an Exception... then throw it?

Catch an Exception... then throw it?

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpquestion
26 Posts 17 Posters 1 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 doja93

    An inexperienced developer I use to work with kept telling me that she, "..wanted to add exception handling.." to her code. For various reasons, I wasn't quite sure what she meant by that and for other reasons, I didn't bother asking... A couple weeks later, I did a diff on one of her check-ins to find that she had gone into a particular class (C#) and added try/catch blocks to every function in the class that looked like this:

    public void FunctionName()
    {
    try
    {
    // code that was previously contained in the function...
    }
    catch (Exception e)
    {
    throw e
    }
    }

    A Offline
    A Offline
    Adriaan Davel
    wrote on last edited by
    #21

    I think

    doja93 wrote:

    she, "..wanted to add exception handling.." to her code

    already show she has potential, just need some guidance... I've seen very few developers who WANT to add exception handling to their code... :) I did this article, readers seemed to have found it informative: Exception Concepts for Business Applications I'm way over due to continue with a second article, I know...

    ____________________________________________________________ Be brave little warrior, be VERY brave

    M 1 Reply Last reply
    0
    • M mohan5k

      HI, I do not really understant why we have to catch the exception and throw it. I mean if a (first)function/sub is caling another (Second)function/sub if any exception is coming in sencond function why we have to catch it and throw it. It will automatically throw the exception right?

      function1()
      {
      try
      {
      calling function2()
      }
      catch(exception e)
      {
      //I handled the exception thrown from the functions/subs called
      }

      }

      function2()
      {
      //if any error/exception comes here .net handler automatically throws it to the calling function/sub where it was handled.
      }

      modified on Monday, November 29, 2010 7:18 AM

      R Offline
      R Offline
      Rod Kemp
      wrote on last edited by
      #22

      mohan5k wrote:

      I do not really understand why we have to catch the exception and throw it.

      For your own code where you write the functions and you call them and you know you will catch and handle any exceptions it is not required that in your example function2 has a try/catch that re-throws the exception. Where it does come in handy is when you are writing code that other people will use, in this case you may want to log all errors do you trust that the people using your code will do this for you or do it correctly, no you don't, you catch all errors log them then re-throw them back to the calling code or you may only want to re-throw errors under specific conditions such as if you are catching the SQLExceptions and get SQL Error 1205 (deadlock) you may want to retry the operation where as all other errors you may throw back to the calling code. How you use it depends on what you are doing and how you expect it to be handled.

      People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs

      M 1 Reply Last reply
      0
      • R Rod Kemp

        Meh, catching and re-throwing exceptions does have its uses, just not with throw e. Say if you want to log the exception and don't trust that whoever wrote the calling function will do it, you could write;

        try
        {
        // code that may throw exception here
        }
        catch(Exception ex)
        {
        // error logging here
        // re-thrown the exception
        throw;
        }

        Or if you catch multiple exceptions and want to re-throw those you don't want to handle like;

        try
        {
        // code that may throw exception here
        }
        catch(SQLException sqlex)
        {
        //Handle this exception here
        //Don't re-throw the exception
        }
        catch
        {
        throw; //re-throw all other exceptions that may occur
        }

        What this actually is, is a chance for you to mentor an inexperienced developer on the correct way to use try/catch blocks.

        People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #23

        If he doesn't even know what she meant by 'add exception handling' - how much do you think he is going to be able to tutor her

        Life goes very fast. Tomorrow, today is already yesterday.

        1 Reply Last reply
        0
        • D doja93

          An inexperienced developer I use to work with kept telling me that she, "..wanted to add exception handling.." to her code. For various reasons, I wasn't quite sure what she meant by that and for other reasons, I didn't bother asking... A couple weeks later, I did a diff on one of her check-ins to find that she had gone into a particular class (C#) and added try/catch blocks to every function in the class that looked like this:

          public void FunctionName()
          {
          try
          {
          // code that was previously contained in the function...
          }
          catch (Exception e)
          {
          throw e
          }
          }

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #24

          I think this is not real. Understandable you cut the actual do something code but where is your semi-colon after the 'throw e' I assume you typed this code, maybe you missed an important bit of catch code

          Life goes very fast. Tomorrow, today is already yesterday.

          1 Reply Last reply
          0
          • R Rod Kemp

            mohan5k wrote:

            I do not really understand why we have to catch the exception and throw it.

            For your own code where you write the functions and you call them and you know you will catch and handle any exceptions it is not required that in your example function2 has a try/catch that re-throws the exception. Where it does come in handy is when you are writing code that other people will use, in this case you may want to log all errors do you trust that the people using your code will do this for you or do it correctly, no you don't, you catch all errors log them then re-throw them back to the calling code or you may only want to re-throw errors under specific conditions such as if you are catching the SQLExceptions and get SQL Error 1205 (deadlock) you may want to retry the operation where as all other errors you may throw back to the calling code. How you use it depends on what you are doing and how you expect it to be handled.

            People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs

            M Offline
            M Offline
            mohan5k
            wrote on last edited by
            #25

            I know what you are tyring to say.. I just commented for the above post as they simply catching and throwing it again with out doing any work..

            1 Reply Last reply
            0
            • A Adriaan Davel

              I think

              doja93 wrote:

              she, "..wanted to add exception handling.." to her code

              already show she has potential, just need some guidance... I've seen very few developers who WANT to add exception handling to their code... :) I did this article, readers seemed to have found it informative: Exception Concepts for Business Applications I'm way over due to continue with a second article, I know...

              ____________________________________________________________ Be brave little warrior, be VERY brave

              M Offline
              M Offline
              mindserve
              wrote on last edited by
              #26

              That's the best answer I have seen yet. sometimes writing code is not about making it work, but how to handle things when they don't work....... :laugh:

              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