Understanding the role of exceptions in programming. 🛠️💡
1. Base Exception Class:-
The base class for all exceptions.
Typically not directly thrown; serves as a base class for specific exceptions.
2. Invalid Argument:-
Thrown when one of the arguments provided to a method is not valid.
Example:-
void DoSomething(int value)
{
if (value < 0)
{
throw new ArgumentException("Value must be non-negative", nameof(value));
}
// Method logic
}
3. Null Argument:-
Thrown when one of the arguments provided to a method is not valid.
Example:-
void DoSomething(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text), "Text cannot be null");
}
// Method logic
}
4. Argument Out of Range:-
Thrown when an argument is outside the allowable range.
Example:-
void DoSomething(int value)
{
if (value < 1 || value > 10)
{
throw new ArgumentOutOfRangeException(nameof(value), "Value must be between 1 and 10");
}
// Method logic
}
5. Null Reference:-
Thrown when trying to access a member on a null object.
Example:-
string text = null;
int length = text.Length; // Throws NullReferenceException
6. Divide By Zero:-
Thrown when attempting to divide an integer or decimal by zero.
Example:-
int result = 10 / 0; // Throws DivideByZeroException
7. Index Out of Range:-
Thrown when trying to access an index outside the bounds of an array or collection.
Example:-
int[] numbers = { 1, 2, 3 };
int value = numbers[5]; // Throws IndexOutOfRangeException
8. Format Error:-
Thrown when the format of an argument is invalid.
Example:-
string str = "ABC";
int num = int.Parse(str); // Throws FormatException
9. Invalid Cast:-
Thrown when an explicit conversion between types is invalid.
Example:-
object obj = "Hello";
int num = (int)obj; // Throws InvalidCastException
10. I/O Exception:-
The base class for exceptions related to input/output operations. Often occurs in file operations, network communication, etc.
11. File Not Found:-
Thrown when attempting to access a file that does not exist.
Example:-
try
{
string content = File.ReadAllText("nonexistent_file.txt"); // Throws FileNotFoundException
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"File not found: {ex.Message}");
}
12. Not Supported:-
Thrown when an operation is not supported.
Example:-
var unsupportedCollection = new ReadOnlyCollection<int>(new List<int>());
unsupportedCollection.Add(5); // Throws NotSupportedException
13. Overflow:-
Thrown when an arithmetic operation exceeds the bounds of the data type.
Example:-
checked
{
int maxIntValue = int.MaxValue;
int result = maxIntValue + 1; // Throws OverflowException
}
14. Timeout:-
Thrown when an operation times out.
Example:-
try
{
// Perform an operation that has a timeout
}
catch (TimeoutException ex)
{
Console.WriteLine($"Operation timed out: {ex.Message}");
}
15. Platform Not Supported:-
Thrown when an operation is not supported on the current platform.
Example:-
if (!Environment.OSVersion.Platform.Equals(PlatformID.Win32NT))
{
throw new PlatformNotSupportedException("This operation is only supported on Windows.");
}
16. Security Violation:-
Thrown when a security violation is detected. Often related to security-related operations and permissions.
17. Type Initialization Failure:-
Thrown when the initialization of a type fails.
18. HTTP-related Exception:-
Used in ASP.NET applications to represent HTTP-related errors. Commonly encountered in web applications.
#ExceptionHandling #ErrorHandlingInCode #ProgrammingTips #SoftwareDevelopment #ExceptionTypes#sparkleweb #suratithub
Dipak Pakhale
A skilled .Net Full Stack Developer with 8+ years of experience. Proficient in Asp.Net, MVC, .Net Core, Blazor, C#, SQL, Angular, Reactjs, and NodeJs. Dedicated to simplifying complex projects with expertise and innovation.
Reply