Loading ...

How to handle exceptions in ASP.NET | CodeAsp.Net

How to handle exceptions in ASP.NET

 /5
0 (0votes)

If you are developing an application, you should prepare for exceptions in your application. Exceptions are part of the applications. An exception means an error and it is run time error. In this blog I will explain how to avoid these unwanted exceptions in your applications.

Exceptions – As I already told you that exception is not a compile time error. Exception is a run time error. We can’t program for exception because exception occurs from your logic mistake and etc. But we can avoid dead screens and user can see custom error page. Take advantages of Exception handling, we can get and handle run time errors. For examples, validating user input, checking for null object, etc are all examples of great standard Exception handling. System.Exception is the base class for all the exceptions. 

Category of Exception

  1. SystemException( pre-defined common language runtime exception classes derived from SystemException.)
  2. ApplicationException (user-defined application exception classes derived from ApplicationException.)

List of the some useful properties of Exception –

pro

List of the some useful public methods of Exception –

pubmrth

As I above told you that when exceptions are thrown, you need to be able to handle them. There is a try-catch-finally blocks great feature of C Sharp. try-catch-finally blocks are able to handle exceptions. Code that could throw an exception is put in the try block an exception handling code goes in the catch block. There can be one or more catch blocks in try-catch-finally. If we want some code every time execute whether exception might occur or not, in this situation we can write our code in finally block. Below I am giving a simple example of try-catch. In below example, if user click on button without enter value in textbox, it would give an error, else it will run successfully.

Example - 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
        <asp:Button ID="btnTest" runat="server" onclick="btnTest_Click" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ExceptionInASPNET : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnTest_Click(object sender, EventArgs e)
    {
        try
        {
            int x = Convert.ToInt32(txtTest.Text);
            int y = 5;
            int sum = x + y;
            Response.Write("Sum is :" + sum);
        }
        catch (Exception ex)
        {
            Response.Write("Exception is : " + ex.ToString());
        }
        finally
        {
            Response.Write(" Welcome.This finally block is every time excuted. " );
        }
    }
}

Now, you should implement your code in try-catch blocks that handle exceptions and also clean up resources by implementing a finally block.
Hope this helps you. 

 

Comments (1)

   
Dear very nice example, i am to happy today to clear my concept, Actully i already knew the difference between eval and bind and i also have an example in my mind that how to deal with eval() and bind(), so by seeing example,i got too much understanding and you implement difference according to my mind.. Any way thankx very,
11/11/2010
 · 
by
  • :*
  • :*
  • :
 *

Top Posts