Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 4.15 KB

Exceptions and Error Handling.md

File metadata and controls

77 lines (52 loc) · 4.15 KB

Exceptions and Error Handling

Simple Explanation

In C#, exceptions are a way to handle runtime errors that may occur during the execution of your program. Exceptions are used to signal an error condition that your code should handle gracefully. When an error occurs, C# generates an exception object, which contains information about the error, such as the type of error, the location of the error, and a stack trace of the method calls that led up to the error.

Deep Explanation

C# provides several mechanisms for handling exceptions, including try-catch-finally blocks, throw statements, and custom exception classes. When an exception is thrown, C# searches for the nearest try-catch-finally block that can handle the exception. If a matching catch block is found, the code inside the catch block is executed to handle the error condition. If no matching catch block is found, the exception is propagated up the call stack until it is either caught by a higher-level try-catch block or it causes the program to terminate.

The try-catch-finally block is the most common way of handling exceptions in C#. It consists of a try block that contains the code that may throw an exception, one or more catch blocks that handle specific types of exceptions, and an optional finally block that contains cleanup code that is always executed, regardless of whether an exception is thrown or not.

In addition to the built-in exceptions that come with C#, you can also create your own custom exception classes by deriving from the Exception class. This allows you to define your own types of exceptions that are specific to your application or domain.

Examples

In this example, the try block contains the code that may throw an exception. In this case, we're attempting to divide 10 by zero, which will result in a DivideByZeroException being thrown. The catch block specifies the type of exception that it can handle (in this case, DivideByZeroException), and contains the code that handles the exception. In this case, we're simply writing a message to the console that explains what went wrong.

try 
{
    int x = 10 / 0; // This will cause a divide by zero exception
}
catch (DivideByZeroException ex) 
{
    Console.WriteLine("An error occurred: " + ex.Message);
}

In this example, we've created a custom exception class called MyException that derives from the Exception class. We've also created a method called DoSomething that throws an instance of the MyException class with a custom error message. In the try-catch block, we're catching instances of the MyException class and writing the error message to the console.

public class MyException : Exception 
{
    public MyException(string message) : base(message) 
    {
    }
}

public void DoSomething() 
{
    throw new MyException("Something went wrong!");
}

try 
{
    DoSomething();
}
catch (MyException ex) 
{
    Console.WriteLine("An error occurred: " + ex.Message);
}

Common types of Exception

As a .NET developer, you should be familiar with the most common types of exceptions that can be thrown by .NET Framework classes or your own code. Here are some of the most common exception types:

  • ArgumentException: This exception is thrown when one of the arguments passed to a method is invalid.

  • ArgumentNullException: This exception is thrown when a null argument is passed to a method that doesn't accept null values.

  • InvalidOperationException: This exception is thrown when an operation is not valid for the current state of an object.

  • IndexOutOfRangeException: This exception is thrown when an index is outside the valid range of an array or collection.

  • NullReferenceException: This exception is thrown when you try to access a member of a null object reference.

  • OutOfMemoryException: This exception is thrown when there is not enough memory available to perform an operation.

  • OverflowException: This exception is thrown when an arithmetic operation results in an overflow.

  • FormatException: This exception is thrown when a string cannot be converted to a numeric value.

  • FileNotFoundException: This exception is thrown when a file cannot be found.

  • IOException: This exception is thrown when an I/O error occurs.