advertisement
Properties and Methods of the Exception Class in C#
The System.Exception class is the base class that allows handling all exceptions in c#. This means that all exceptions in c# inherit the System.Exception class either directly or indirectly. The System.Exception class contains public and protected methods that can be inherited by other exception classes. In addition, The System.Exception class contains properties that are common to all exceptions.
Properties:
- Message: Displays a message that indicates the reason for the exception.
- Source: Provides the name of the application or the object that caused the exception.
- StackTrace: Provides exception details on the stack at the time the exception was thrown.
- InnerException: Returns the Exception instance that caused the current exception.
Methods:
- Equals: Determines whether objects are equal.
- GetBaseException: Returns a type of exception class when overridden in a derived class.
- GetHashCode: Returns a hash function for a particular type.
- GetObjectData: Stores information about serializing or deserializing a particular object with information about the exception when overridden in the inheriting class.
- GetType: Retrieves the type of the current instance.
- ToString: Returns a string representing the thrown exception.
Commonly Used Exception Classes:
The System.Exception class has several derived exception classes to handle different types of exceptions.
- System.ArithmeticException: This exception is thrown for problems that occur due to arithmetic or casting and conversion operations.
- System.ArgumentException: This exception is thrown when one of the arguments does not match the parameter specifications of the involved method.
- System.DivideByZeroException: This exception is thrown when an attempt is made to divide a numeric value by zero.
- System.IndexOutOfRangeException: This Exception is thrown when an attempt is made to store data in an array using an index that is less than zero or outside the upper bound of the array.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0