Resolved What an exception class should contain?

Sajo

Member
Joined
Jul 22, 2020
Messages
17
Programming Experience
Beginner
I am currently learning to create my own exception class and am wondering what my exception class should contain? It confuses me a lot. If anyone can give me an example. I need help urgently
 
Solution
The base Exception class already have many private fields to hold state and properties to expose them. Most derived Exception classes only exist "by name" to recognize one type of exception from another, and only contains constructors to pass various data in (since constructors are not inherited) and maybe private fields to keep extra state information.
Most of the classes that inherit from Exception do not implement additional members or provide additional functionality; they simply inherit from Exception. Therefore, the most important information for an exception can be found in the hierarchy of exception...
The base Exception class already have many private fields to hold state and properties to expose them. Most derived Exception classes only exist "by name" to recognize one type of exception from another, and only contains constructors to pass various data in (since constructors are not inherited) and maybe private fields to keep extra state information.
Most of the classes that inherit from Exception do not implement additional members or provide additional functionality; they simply inherit from Exception. Therefore, the most important information for an exception can be found in the hierarchy of exception classes, the exception name, and the information contained in the exception.
"information contained in the exception" here means the string message, stacktrace and/or inner exceptions it holds - all part of base Exception class. (the content of the string message will have a meaning of course)
 
Solution
And as for the meta question, the exception should contain enough information for the programmer writing the calling code which causes an exception has enough information to be able to diagnose what went wrong, and hopefully how to fix it.
 
If anyone can give me an example.
Why would you need us to provide an example when every exception class already part of the .NET Framework is an example? Have you studied those existing classes to see what they contain? If not, why not? If so, what exactly is confusing to you? If you are trying to represent an exceptional state to the calling code, what exactly about that state do you need to convey? You're the one who knows that - not us - so you tell us: what have you got that the exception class could contain? If you don't know that then why are you even trying to define an exception class in the first place? If you can tell us what you're trying to express then we might be able to help with an explanation of what form it should take but, as it is, your question is extremely vague.
 

Latest posts

Back
Top Bottom