Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Class: When to Derive from it, In C# (.Net)?

Tags:

c#

.net

exception

I am continuing with my exam revision.

I have come across the usage of the Base Exception class and I have seen it on exam papers also.

My question is when do you derive from the Base Exception class?

I am of the impression if you want a custom class to throw an exception with more meaningful information, then you can create a custom exception class that contains the exact data that is representative of how your custom class is used and what scenario it is designed to be used for?

Why can't my custom exception class derive from 'ApplicationException' or 'SecurityException' or the base 'Exception' class?

I am of the impression that I should derive from the base Exception class and not the previous two.

My question second is, when would you derive from the other two??? Are there any clear-cut distinctions as to when you would derive from either one of these three? Assuming there are no others I have I have missed out?

SMALL UPDATE:

This question from transcender pretty much hits the nail on the head.


*Which class should you use to generate an application-specific exception?

Answer: The ApplicationException class*

like image 977
IbrarMumtaz Avatar asked Jan 22 '23 08:01

IbrarMumtaz


1 Answers

This is all discussed in the Design Guidelines document.

like image 98
Cheeso Avatar answered Jan 28 '23 17:01

Cheeso