Custom Exceptions
-
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`?
-
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`?
Yes. But not frequently. I don't know what you mean by "how they look".
-
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`?
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".
-
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`?
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!
-
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`?
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 likeObjectNotFoundException
andNoPrivilegeException
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
-
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`?
RandyBuchholz wrote:
By how they look, I mean do you keep them basic and mostly use
message
, or do you do things like populatedata
?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
-
RandyBuchholz wrote:
By how they look, I mean do you keep them basic and mostly use
message
, or do you do things like populatedata
?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
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.
-
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`?
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!
-
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`?
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.
-
RandyBuchholz wrote:
By how they look, I mean do you keep them basic and mostly use
message
, or do you do things like populatedata
?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
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
-
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`?
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