i

ASP.Net A Complete Guide

Error Handling

What happens if you are doing some important transactions and suddenly our application breaks, then it will be a very bad impression to the end-user. Our application should work smoothly in any situation. If any transaction fails, then there should be the proper way to handle this, and more information should be logged in the log file. At the same time, the user needs to see some custom message that the last operation is not completed or failed.

This handling the error should be consistent for great user experience as well as for the important transaction. ASP.NET has provided a great feature to handle that kind of situation. "Exception" class is the base class of every exception.

Exception Handling can be done in one of the levels given below:

  • Application-level

  • Page-level

  • Code-level

Application Level Error Handling:

We can handle default errors at the application level either by modifying our application's web.config or by modifying the Application_Error handler in the Global.asax file.

Modifing web.config file:

 

 

     

   

 

  Modifing Global.asax file:

void Application_Error(object sender, EventArgs e)

{

    Exception ex = Server.GetLastError();

 

    if (ex is HttpUnhandledException)

    {

        Server.Transfer("LogError.aspx?handler=Application_Error%20-%20Global.asax", true);

    }

}

 

 

Page-Level Error Event Handling:

page-level error handler helps us to log the unhandled exception. We can also take the user to a page that can display helpful information.

private void Page_Error(object sender, EventArgs e)

{

    Exception ex = Server.GetLastError();

 

    // Handle specific exception.

    if (ex is HttpUnhandledException)

    {

        ErrorMsgTextBox.Text = ""

    }

    // Clear the error from the server.

    Server.ClearError();

}

 

 

Code Level Error Handling:

We can achieve this by using a try-catch-finally block. We can do this for each and every function of the application. This will help us to identify the specific exception because, in the exception stack trace, we can have all the details regarding the error. Details like file name, function name, line number, etc.

 

Syntax:

try

{

    // code that may raise exceptions

}

catch(Exception ex)

{

    // handle exception

}

finally

{

    // final cleanup code

}

 

 

Example :

try

{

    using (StreamReader sr = File.OpenText("details.txt"))

    {

        Console.WriteLine($"Read this file is {sr.ReadLine()}");

    }

}

catch (FileNotFoundException ex)

{

    Console.WriteLine($"The file was not found: '{ex}'");

}

finally

{

    if (sr != null)

    {

        sr.Close();

    }

}

 

In the above example, if the file is not found, then the application will throw the exception, then that can be achieved by the catch block. As soon as code throws the exception, the execution stops that point only, and the catch block starts execution.

Finally, the block is used to release the managed resources in both scenarios. Both the scenario means it will execute when there is no exception, and when there is an exception.