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. General Programming
  3. C#
  4. Performace of exceptions?

Performace of exceptions?

Scheduled Pinned Locked Moved C#
questionvisual-studiodesignperformance
6 Posts 6 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.
  • D Offline
    D Offline
    Dr Herbie
    wrote on last edited by
    #1

    Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

    L G K J M 5 Replies Last reply
    0
    • D Dr Herbie

      Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Dr Herbie wrote: What is the performance hit of exceptions, as opposed to returning a null or empty value? Exceptions are VERY expensive, especially the FIRST one (from what I have seen around 600ms). Exceptions are what they are, exceptional conditions (normally from external usage), not some internal bad coding habits. It all depends how your public object/interface will be used. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

      1 Reply Last reply
      0
      • D Dr Herbie

        Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

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

        It's recommended that you use as few exceptions as possbile. An exception should never occur in the normal execution of an application. To use exceptions for the program flow is a lazy way of programming. It's slow, and there is a big risk that you accidentally catch any real errors that might occur, and ignore them. --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • D Dr Herbie

          Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

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

          Another way to look at this is to think in terms of contracts. With the data it's given can a routine fufil its contract, i.e., can it do what it says it can do? If it can then you shouldn't throw an exception. If it can't then it should fail, which means throwing an exception or signalling some other type of failure. Not being able to fulfil its contract is either due to a programmer error, or an environment error such as out of memeory. Either way it will be a rare occurrence. I think in the situation you describe, your gut feeling is probably right. Kevin

          1 Reply Last reply
          0
          • D Dr Herbie

            Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            To add to what has already been said, it is my experience that any method that may throw an exception should have a way of determining whether that exception will be thrown. To use your dictionary as an example... if the index accessor throws an exception when the key doesnt exist then that is fine.. so long as the Contains method always returns false for that key.

            1 Reply Last reply
            0
            • D Dr Herbie

              Hi folks, I recently reviewed some code from a collegue and I found he was using exceptions a great deal -- my gut feeling was that he was using them too much (his dictionary implementations throw exceptions if you pass a key for a non-existent entry, that sort of thing). I set the IDE to break whenever an exception is thrown and I got bored after the 20th exception before the UI even displayed. So my question is: What is the performance hit of exceptions, as opposed to returning a null or empty value? Any pointers to articles would be especially useful as I will probably need hard data to win the argument. Thanks Herbie Dr Herbie Remember, half the people out there have below average IQs.

              M Offline
              M Offline
              Matt Gerrans
              wrote on last edited by
              #6

              Here's a chapter from a design book on the subject Exceptions[^]. Also, Effective Java and More Effective C++ have good material. I have't seen Effective C#, but I suspect it has a similar take on them and they will all support your argument. I'm not sure the performance angle is the best one to pursue though. I think it is better to point out that it is simply misuse of the feature. The purpose of exceptions is to deal with exceptional conditions. Matt Gerrans

              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