C# Exception Handling Keywords
In C#, we use 4 keywords to perform exception handling:
o try
o catch
o finally, and
o throw
C# | Exception Class
Exception Hierarchy
Different Exception Classes:
There are different kinds of exceptions which can be generated in C# program:
Divide By Zero exception: It occurs when the user attempts to divide by
zero
Out of Memory exceptions: It occurs when then the program tries to use
excessive memory
Index out of bound Exception: Accessing the array element or index
which is not present in it.
Stackoverflow Exception: Mainly caused due to infinite recursion process
Null Reference Exception : Occurs when the user attempts to reference
an object which is of NULL type.
…..and many more.
C# SystemException Constructors
Constructors Description
SystemException() It is used to initialize a new instance of
SystemException class.
SystemException(SerializationInfo,StreamingContext) It is used to initialize a new instance of
SystemException class with serialized data.
SystemException(String) It is used to initialize a new instance of
SystemException class with a specified error messag
SystemException(String,Exception) It is used to initialize a new instance of
SystemException class with a specified error me
and a reference to the inner exception that is the
of this exception.
C# SystemException Properties
Property Description
Data It is used to get a collection of key/value pairs that provide additional user-defined inform
about the exception.
HelpLink It is used to get or set a link to the help file associated with this exception.
HResult It is used to get or set HRESULT, a coded numerical value that is assigned to a sp
exception.
InnerException It is used to get the Exception instance that caused the current exception.
Message It is used to get a message that describes the current exception.
Source It is used to get or set the name of the application that causes the error.
StackTrace It is used to get a string representation of the immediate frames on the call stack.
TargetSite It is used to get the method that throws the current exception.
C# SystemException Methods
Method Description
Equals(Object) It is used to check that the specified object is equal t
current object or not.
Finalize() It is used to free resources and perform cle
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 instan
MemberwiseClone() It is used to create a shallow copy of the current Objec
ToString() It is used to create and return a string representation
current exception.