Exceptions Handling in C#.Net

What is exception how to handle in C#.net. We have different type of exception for handling it use try, catch and finally on error best practices.
In today's article I will show you simple tutorial with example for best way to handle exception while working wiles with files in .NET core using c#.net. In this tutorial we will cover following topics like C# exception handling best practices, C# catch multiple exceptions, throw exception C# and many more.

What is Exception in C#.Net?

Exception in .NET core, c#.net describe that the written code having error at run time and block the code for further execution. Exception helps us to understand that where are the problem is there in out written code. Exception in .NET core using c#.net we can handle with the try, catch and finally block. This helps us to manage the error occurred during the execution of the code. 

Exception can be generated by the CLR, by .Net library, any third-party library referred in your application code. Exception can be created by throw keyword. Here i am going to demonstrate a simple tutorial with an example to throw the exception. As we know exception are of different type and all exception are driven from System.Exception.
int value1 = 200;
int value2 = 0;
int calculatedValue = value1 / value2; 
In above code if we execute, we will receive the exception "Attempted to divide by zero.'.

Divided by zero exception

How to handle exception in C#.Net?

By using try, catch and finally block we handle the exception in C#.net. To handle the exception, we will put our code in try block and after that put the catch block to capture the exception. At the end we will put the finally block. Please check the Syntex of try catch and finally block.
 try{
   //Your code block
}
Catch(Exception ex)
{
   //Code to capture the exception detail
}
finally{

}
In above code as mentioned the try block is used to place your code which may throw the exception. After try block catch block have been placed, this block will provide the detail about the exception. In this block we can capture the exception detail and place in the log file or in the data base. Here finally block in the block which will definitely execute does not matter exception execute or not. 

Type of Exceptions?

Few of the common exception in .net core Exception, IndexOutOfRangeException, NullReferenceException, InvalidOperationException, ArgumentException, ArgumentNullException and ArgumentOutOfRangeException. If we are working on files here the common type of exception occured while working on files FileNotFoundException, DirectoryNotFoundException and IOException. Now lets understand about each type of exception in short. 
  • Exception: This is the most commonly used to capture the exception. Exception is the base class for all the exceptions.
  • IndexOutOfRangeException: Whenever we are using array in out code and trying teh access the value from array from a specific index and there is not value at on specified index of array we will ger the index out of range exception.
  • NullReferenceException: This exception occurs whenever we try to refer a null object. Null refrence exception occured at run time.
  • InvalidOperationException: This exception occurs in a metho in an invalid state. 
  • ArgumentException: This exception occurs whenever we pass an argument value other than the expected value in in the argument. For example, you have written a piece of code to add two numbers and in input one value passed as string, on that case we will receive the exception. 
  • ArgumentNullException: This exception occurs when a method or a function does not allow the null value for its' arguments. 
  • ArgumentOutOfRangeException: This exception occurred whenever the pased parameter to a method or a function is not as it defined. 
  • FileNotFoundException: This exception whenever we are trying to access a file from a directorryy and it is not available. 
  • DirectoryNotFoundException: This exception occurs when we try to access a directory or try to read it is not available on the provided location. 
  • IOException: This exception occurs whenever we try to access or read or write a file ant it is not accessible IO exception will occur.
  • AuthenticationException: This exception we can throw whenever exception occurs while authenticating the user. 

Best Practices of Exception Handling in C#.Net.

So, we all know what exception is and what re the different type of exception in C#.Net or in .NET core application. But what are the best practices for the exception handling. 
  • To handle the exception always use the try catch block. 
  • Never apply try catch block just to bypass the exception
  • In Catch block always capture the exception and put it in log for further analysis or to validate the cause of the exception
  • In finally block always dispose the objects or any operation which has to execute after completion of the process.
  • Always try to handle exception as per the type of exception for example if you are currently performing files operation and, in your try, catch block you can use FileNotFoundException , DirectoryNotFoundException, IOException.

Post a Comment