C Sharp SystemException class

C# SystemException class

C# provides a predefined exception class to handle the system-related exceptions. This exception class is named as the SystemException class to work as a base class for the system exception namespace. The ValidationException, ArgumentException, ArithmeticException, DataException, StackOverflowException, and many more exceptions. are the various child classes of the SystemException class. Various constructors, properties and methods are also included in the C# SystemException class.

C# SystemException Signature:

  • [SerializableAttribute]
  • [ComVisibleAttribute(true)]
  • public class SystemException : Exception

C# SystemException Constructors:

Constructors Uses
SystemException() To initialize a new instance of the SystemException class.
SystemException(SerializationInfo,StreamingContext) To initialize a new instance of the SystemException class with serialized data.
SystemException(String) To initialize a new instance of the SystemException class with a specified error message.
SystemException(String,Exception) To initialize a new instance of the SystemException class with a specified error message and a reference to the inner exception which is the cause of this exception.

C# SystemException Properties:

Property Uses
Data To return a collection of key/value pairs. It provides additional user-defined information about the exception.
HelpLink To return or to set a link to the help file associated with the exception.
HResult To return or to set HRESULT. The HRESULT is a coded numerical value assigned to a specific exception.
InnerException To return the Exception instance that caused the current exception.
Message To return a message describing the current exception.
Source To return or to set the name of the application causing the error.
StackTrace To return a string representation of the immediate frames on the call stack.
TargetSite To return the method throwing the current exception.

C# SystemException Methods:

Method Uses
Equals(Object) To verify whether the specified object is equal to the current object or not.
Finalize() To free the resources and to perform cleanup operations.
GetBaseException() To return the root exception.
GetHashCode() To return the hash code.
GetObjectData(SerializationInfo,StreamingContext) To return the object data.
GetType() To return the runtime type of the current instance.
MemberwiseClone() To create a shallow copy of the current Object.
ToString() To create and to get a string representation of the current exception.

Example:

using System;  
namespace Example  
{  
    class Csharp  
    {  
        static void Main(string[] args)  
        {  
            try  
            {  
                int[] a = new int[10];  
                a[20] = 100;  
            }  
            catch (SystemException e)  
            {  
                Console.WriteLine(e);  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, the SystemException class is used to handle the exception of subclasses. Here, we defined the size of the created array to be 10. But we later defined the value at the 20th index of the array which is not available and is out of range of the size defined for the array. The IndexOutOfRangeException is thus generated. This exception is a subclass of the SystemException class.

Please follow and like us:
Content Protection by DMCA.com