List down the commonly used type of exceptions?
In C#, exceptions are used to handle and communicate error conditions that occur during the execution of a program. Some commonly used types of exceptions in C# include:
1-ArgumentNullException:
Thrown when a method or constructor receives a null argument that it cannot handle.
2-ArgumentException:
Thrown when an argument passed to a method is invalid or outside the expected range.
3-InvalidOperationException:
Thrown when an operation is performed on an object in an invalid state.
4-NotSupportedException:
Thrown when an operation is not supported by the current implementation or configuration.
5-FormatException:
Thrown when a format of a string or input is invalid or cannot be parsed correctly.
6-IndexOutOfRangeException:
Thrown when an array or collection index is outside the valid range.
7-DivideByZeroException:
Thrown when an attempt is made to divide an integer or decimal number by zero.
8-FileNotFoundException:
Thrown when an attempt is made to access a file that does not exist.
9-IOException:
Thrown when an input or output error occurs while accessing a file or stream.
10-InvalidOperationException:
Thrown when an operation is not valid for the current state of an object.
11-OverflowException:
Thrown when an arithmetic operation results in an overflow or underflow.
12-NullReferenceException:
Thrown when a null reference is accessed, i.e., when an object reference points to null.
These are just some of the commonly used exception types in C#. However, C# provides a wide range of built-in exception types, and you can also create custom exception types by deriving from the Exception base class or one of its derived classes.
When handling exceptions, it's important to catch specific exceptions that you anticipate and handle them appropriately to provide meaningful error messages or take corrective actions in your code.