Exception handling in asp.net - Part 69

Suggested Videos
Part 66 - SQLServer asp.net session state mode management
Part 67 - Asp.net Application state
Part 68 - Asp.net application state real time example

In this session we will discuss about handling errors using try/catch blocks. In a later video session we will discuss about, how exceptions are generally handled in a real time asp.net application.

Exceptions are unforeseen errors that happen within the logic of an application. For example, when reading a file, a number of exception conditions can occur.
1. The file might not exist
2. You may not have permissions to access the file



When an exception occurs and if it is not handled, then, that exception is called as an, unhandled exception. An unhandled exception is displayed to the user using an "yellow screen of death". Displaying the screen of death is bad for 2 reasons
1. The error messages are cryptic and may not make any sense to the end user
2. The exception information may be useful for a hacker, to hack into your application

Exception handling using - try-catch 
try - Wrap the code in a try block that could possibly cause an exception. If a statement in the try block causes an exception, the control will be immediately transferred to the catch block.

catch - catches the exception and tries to correct the error and/or handles the exception

finally - Used to free resources. Finally block is guaranteed to execute irrespective of whether an exception has occurred or not

throw - used to raise an exception

The base class for all exceptions is the Exception class. Specific exceptions should be caught, before catching the general parent exception.



Create an asp.net web application, and add an xml file, Countries.xml. Copy and paste the following XML.
<?xml version="1.0" encoding="utf-8" ?>
<Countries>
  <Country>
    <Id>101</Id>
    <Name>India</Name>
    <Continent>Asia</Continent>
  </Country>
  <Country>
    <Id>102</Id>
    <Name>UK</Name>
    <Continent>Europe</Continent>
  </Country>
  <Country>
    <Id>103</Id>
    <Name>US</Name>
    <Continent>North America</Continent>
  </Country>
  <Country>
    <Id>104</Id>
    <Name>France</Name>
    <Continent>Europe</Continent>
  </Country>
</Countries>

Drag and drop, gridview and a lable control on WebForm1.aspx. HTML of the aspx page.
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblError" Font-Bold="true" ForeColor="Red" runat="server"></asp:Label>

WebForm1.aspx.cs Code:
public partial class WebForm1 : System.Web.UI.Page
{
    // Uncomment this line to print the name of the account 
    // used to run the application code
    // Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("~/Countries.xml"));
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        // Catch specific exceptions first
        catch (System.UnauthorizedAccessException unauthorizedAccessException)
        {
            //Log the exception information
            lblError.Text = "Access to the file denied";
        }
        catch (System.IO.FileNotFoundException fileNotFoundException)
        {
            //Log the exception information
            lblError.Text = "File does not exist";
        }
        catch (Exception ex)
        {
            //Log the exception information
            lblError.Text = "There is an unkown problem. IT team is working on this issue. Please check back after some time";
        }
        finally
        {
            // Code to clean up resources like closing file handles
            // and database connection objects
        }
    }
}

No comments:

Post a Comment

It would be great if you can help share these free resources