Throwing Exceptions in C#(blackwasp.co.uk)

submitted by BlackWaspBlackWasp(4024) 4 years, 10 months ago

The thirty-fifth part of the C# Fundamentals tutorial completes an investigation of exception handling. In this article we will consider the throwing of exceptions to report error conditions. This includes the use of standard and custom exception types.

3 comments |category: |Views: 183

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by yesthatmcgurkyesthatmcgurk(4063) 4 years, 10 months ago 0

Eh. Very basic; also, extending the ApplicationException object or any other exception type other than System.Exception is considered bad practice in general. Custom exceptions should almost always extend System.Exception because if you extend any other exception type you risk callers catching your exception unintentionally when they catch your chosen base type. In this code, if you had a caller that was catching ApplicationException, they would (possibly) unintentionally catch your exception type. Only when you specifically want this kind of behavior is when you should extend a different base extension type other than System.Exception.

Reply

posted by BlackWaspBlackWasp(4024) 4 years, 9 months ago 0

An interesting comment, thank you. The article is supposed to be basic, it is a part of a large tutorial for beginners.

When I wrote this one I serious pondered over the extension of System.Exception or ApplicationException. SOME people consider it bad practice to extend ApplicationException. Indeed this is written about in the personal blog of at least one person at Microsoft. This is usually to keep the exception hierarchy flat. However, Microsoft also suggest within their own C# documentation that you should not extend the System.Exception. A case of one company giving to opposing views. In the end, I believe that both methods can be appropriate and that extending the ApplicationException to disassociate application-thrown rather than system-thrown exceptions is valid.

Reply

posted by BlackWaspBlackWasp(4024) 4 years, 7 months ago 0

I have now added a final paragraph to the article regarding this. It is interesting to note that when you read the MSDN topic that their advice is different for differenct .NET versions.

Reply

information Login or create an account to comment on this story