Sunday, June 24, 2012

How to get Type of Exception in C#

Many times we have different ways to handle different kind of errors so we need to know the type of error that has occurred in the system. Here's a simple example in ASP.NET
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["usrPrivilege"].ToString().Equals("USER"))
            {
                //Your Code goes here
            }
        }
        catch (NullReferenceException ex)
        {
            Response.Redirect("~/login.aspx");
        }
        catch (AccessDeniedException ex) 
        {
          //Do something else
        }
        catch (System.Exception ex)
        {
               //Code for other type of exception
        }
    }

No comments: