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. The Lounge
  3. Custom Exceptions

Custom Exceptions

Scheduled Pinned Locked Moved The Lounge
csharpquestion
11 Posts 9 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.
  • R Offline
    R Offline
    RandyBuchholz
    wrote on last edited by
    #1

    Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

    P E OriginalGriffO RaviBeeR M 8 Replies Last reply
    0
    • R RandyBuchholz

      Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

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

      Yes. But not frequently. I don't know what you mean by "how they look".

      1 Reply Last reply
      0
      • R RandyBuchholz

        Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

        E Offline
        E Offline
        Eric Lynch
        wrote on last edited by
        #3

        Mostly, I limit myself to creating custom exceptions for class libraries. Some of the circumstances in which I find them useful... 1) Allow the consumer to easily differentiate and catch my exception. 2) Share additional information with the consumer, usually via properties, without forcing them to de-serialize the message text. 3) The occasional instance when the built-in exceptions don't fit. Not sure what you mean by "how they look".

        1 Reply Last reply
        0
        • R RandyBuchholz

          Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Generally I only do it to differentiate problems my code detects from those the framework does. So if a file doesn't exist, that's not something I have control over - but if there is a problem with data I'm processing directly, the that's a custom exception candidate so it can be trapped as an exception class rather than as part of a generic framework exception group.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • R RandyBuchholz

            Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

            RaviBeeR Offline
            RaviBeeR Offline
            RaviBee
            wrote on last edited by
            #5

            RandyBuchholz wrote:

            Do you use custom exceptions?

            Yes, for situations specific to the business domain of my APIs.  I use standard exceptions like ArgumentNullException where necessary, and business specific exceptions like ObjectNotFoundException and NoPrivilegeException in other cases.

            RandyBuchholz wrote:

            do you keep them basic

            Yes, almost always. /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            1 Reply Last reply
            0
            • R RandyBuchholz

              Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              RandyBuchholz wrote:

              By how they look, I mean do you keep them basic and mostly use message, or do you do things like populate data?

              What OG said. I never populate the exception with data - if useful data is associated with an exception, it gets written out to an error file / table / email / other logging mechanism, by the handler of the exception, never the invoker.

              Latest Article - Building a Prototype Web-Based Diagramming Tool with SVG and Javascript Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

              E J 2 Replies Last reply
              0
              • M Marc Clifton

                RandyBuchholz wrote:

                By how they look, I mean do you keep them basic and mostly use message, or do you do things like populate data?

                What OG said. I never populate the exception with data - if useful data is associated with an exception, it gets written out to an error file / table / email / other logging mechanism, by the handler of the exception, never the invoker.

                Latest Article - Building a Prototype Web-Based Diagramming Tool with SVG and Javascript Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                E Offline
                E Offline
                Eric Lynch
                wrote on last edited by
                #7

                For me, the occasional exception to this rule is a class library for common consumption. As an example, I've had a case where I had an (uncommon) field-level aware consumer. Sharing the name of the offending field, had benefit to the consumer, but no (obvious) benefit to the provider / consumed resource. Other than this edge case, I agree completely with your assertion.

                1 Reply Last reply
                0
                • R RandyBuchholz

                  Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

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

                  Few years back (back in the days of fulltime) I've saw cases where underling juniors too smart for their own good used 'custom exceptions' to achieve [and attempt to hide] GOTO-out-of-a-loop. When confronted, 'where I have goto? you see I doing very properly one.' (It's one of the reasons I avoid public domain source code repositories where anyone can contribute, too many juniors showing off their skills in little known facets of the language often end up producing crap, the above being a regular common example of such.)

                  This internet thing is amazing! Letting people use it: worst idea ever!

                  1 Reply Last reply
                  0
                  • R RandyBuchholz

                    Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

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

                    Add Custom Data to Exceptions -- Visual Studio Magazine[^] I prefer to fill the data over adding extra logging-statements.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    1 Reply Last reply
                    0
                    • M Marc Clifton

                      RandyBuchholz wrote:

                      By how they look, I mean do you keep them basic and mostly use message, or do you do things like populate data?

                      What OG said. I never populate the exception with data - if useful data is associated with an exception, it gets written out to an error file / table / email / other logging mechanism, by the handler of the exception, never the invoker.

                      Latest Article - Building a Prototype Web-Based Diagramming Tool with SVG and Javascript Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #10

                      I'm of the opinion that a library should add as few dependencies as possible. So I'd rather populate the Data property of the exception and send it upwards to a higher level and have it logged there.

                      Wrong is evil and must be defeated. - Jeff Ello

                      1 Reply Last reply
                      0
                      • R RandyBuchholz

                        Languages like C# have enough built in exception types to handle most situations. But you can also create your own. Do you use custom exceptions? If so, what strategies do you use to determine when to use them and how they look? Edit - By how they look, I mean do you keep them basic and mostly use `message`, or do you do things like populate `data`?

                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu Peter
                        wrote on last edited by
                        #11

                        I do create custom exceptions with data, and use them to log whenever they triggered...

                        "The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge". Stephen Hawking, 1942- 2018

                        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                        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