HI WELCOME TO SIRIS

C# SystemException class

Leave a Comment
The SystemException is a predefined exception class in C#. It is used to handle system related exceptions. It works as base class for system exception namespace. It has various child classes like: ValidationException, ArgumentException, ArithmeticException, DataException, StackOverflowException etc.
It consists of rich constructors, properties and methods that we have tabled below.

C# SystemException Signature

  1. [SerializableAttribute]  
  2. [ComVisibleAttribute(true)]  
  3. public class SystemException : Exception  

C# SystemException Constructors

ConstructorsDescription
SystemException()It is used to initialize a new instance of the SystemException class.
SystemException(SerializationInfo,StreamingContext)It is used to initialize a new instance of the SystemException class with serialized data.
SystemException(String)It is used to initialize a new instance of the SystemException class with a specified error message.
SystemException(String,Exception)It is used to initialize a new instance of the SystemException class with a specified error message and a reference to the inner exception that is the cause of this exception.

C# SystemException Properties

PropertyDescription
DataIt is used to get a collection of key/value pairs that provide additional user-defined information about the exception.
HelpLinkIt is used to get or set a link to the help file associated with this exception.
HResultIt is used to get or set HRESULT, a coded numerical value that is assigned to a specific exception.
InnerExceptionIt is used to get the Exception instance that caused the current exception.
MessageIt is used to get a message that describes the current exception.
SourceIt is used to get or set the name of the application that causes the error.
StackTraceIt is used to get a string representation of the immediate frames on the call stack.
TargetSiteIt is used to get the method that throws the current exception.

C# SystemException Methods

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

C# SystemException Example

This class can be used to handle exception of subclasses. Here, in the following program, program throws an IndexOutOfRangeException that is subclass of SystemException class.
  1. using System;  
  2. namespace CSharpProgram  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             try  
  9.             {  
  10.                 int[] arr = new int[5];  
  11.                 arr[10] = 25;  
  12.             }  
  13.             catch (SystemException e)  
  14.             {  
  15.                 Console.WriteLine(e);  
  16.             }  
  17.         }  
  18.     }  
  19. }  
Output:
System.IndexOutOfRangeException: Index was outside the bounds of the array.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.