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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Try Catch.

Try Catch.

Scheduled Pinned Locked Moved C#
performance
9 Posts 5 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.
  • V Offline
    V Offline
    Vipin Venugopal
    wrote on last edited by
    #1

    Do using of so many try-catch block in a project degrade the performance of Project. To be clearer do try catch consume more memory. Vipin

    X G S 3 Replies Last reply
    0
    • V Vipin Venugopal

      Do using of so many try-catch block in a project degrade the performance of Project. To be clearer do try catch consume more memory. Vipin

      X Offline
      X Offline
      XNork
      wrote on last edited by
      #2

      Yes, Try catch is expensive in memmory usage, especially if used frequently.

      _ 1 Reply Last reply
      0
      • X XNork

        Yes, Try catch is expensive in memmory usage, especially if used frequently.

        _ Offline
        _ Offline
        _AK_
        wrote on last edited by
        #3

        Do you know some article which gives a bit details in this regard?

        Best Regards, Apurva Kaushal

        X 1 Reply Last reply
        0
        • V Vipin Venugopal

          Do using of so many try-catch block in a project degrade the performance of Project. To be clearer do try catch consume more memory. Vipin

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Using a try-catch block is not expensive, but when an exception is raised, there is a performance hit. Use try-catch for anything that potentially can go wrong, but don't use it for normal program flow. If you for an example have a string that sometimes contains a number, it's better to check the contents of the string before converting it than to use a try-catch to handle the times when it doesn't contain a number.

          --- b { font-weight: normal; }

          V S 2 Replies Last reply
          0
          • G Guffa

            Using a try-catch block is not expensive, but when an exception is raised, there is a performance hit. Use try-catch for anything that potentially can go wrong, but don't use it for normal program flow. If you for an example have a string that sometimes contains a number, it's better to check the contents of the string before converting it than to use a try-catch to handle the times when it doesn't contain a number.

            --- b { font-weight: normal; }

            V Offline
            V Offline
            Vipin Venugopal
            wrote on last edited by
            #5

            Thank u so much for the information. Vipin

            1 Reply Last reply
            0
            • _ _AK_

              Do you know some article which gives a bit details in this regard?

              Best Regards, Apurva Kaushal

              X Offline
              X Offline
              XNork
              wrote on last edited by
              #6

              http://windowssdk.msdn.microsoft.com/en-us/library/5dws599a.aspx#CodingPractices[^]

              _ 1 Reply Last reply
              0
              • X XNork

                http://windowssdk.msdn.microsoft.com/en-us/library/5dws599a.aspx#CodingPractices[^]

                _ Offline
                _ Offline
                _AK_
                wrote on last edited by
                #7

                Thanks man.. :)

                Best Regards, Apurva Kaushal

                1 Reply Last reply
                0
                • V Vipin Venugopal

                  Do using of so many try-catch block in a project degrade the performance of Project. To be clearer do try catch consume more memory. Vipin

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  Using try/catch, try/finally, and try/catch/finally blocks are only expensive when an exception occurs. At that point, the runtime has to take a lot of steps to unwind all of the catch handlers to see who should respond to the exception. Exceptions should only be thrown when something goes wrong, not to help control program flow. If you use them to try and control program flow, then it gets expensive (not to mention it's just bad practice). Incidentally, if you have any using blocks using (MemoryStream stream = new MemoryStream()) { // do some work here } you are implicitly using a try/finally block. The C# compiler will automatically turn this into MemoryStream stream = new MemoryStream(); try { // do some work here } finally { if (stream != null) { ((IDisposable)stream).Dispose(); } }

                  1 Reply Last reply
                  0
                  • G Guffa

                    Using a try-catch block is not expensive, but when an exception is raised, there is a performance hit. Use try-catch for anything that potentially can go wrong, but don't use it for normal program flow. If you for an example have a string that sometimes contains a number, it's better to check the contents of the string before converting it than to use a try-catch to handle the times when it doesn't contain a number.

                    --- b { font-weight: normal; }

                    S Offline
                    S Offline
                    Scott Dorman
                    wrote on last edited by
                    #9

                    Guffa wrote:

                    If you for an example have a string that sometimes contains a number, it's better to check the contents of the string before converting it than to use a try-catch to handle the times when it doesn't contain a number.

                    In these cases, you should really look at the TryParse methods. .NET 1.1 only had a few of these, but they have been expanded in .NET 2.0 to be included in more of the basic data types.

                    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