Sunday, 24 July 2011

Throwing an Exception using ASP.NET 2.0 and C# .NET


This tutorial will show you how to throw exceptions using ASP.NET 2.0 and C#.NET

Throwing exceptions are easy to do in C# .NET.

In this example we will be throwing exceptions when trying to create a directory in the filesystem, so we will need the System.IO namespace. Our code will catch two exceptions if the user tries to create a directory that is 248 characters long or tries to specify an invalid character within the directory name (such as the asterisk character).

using System.IO;

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

We'll put our code in the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it runs a try block. The try block does two things: it lets exceptions thrown during the try block's execution to be caught by the catch block(s) below and ensures that execution can't leave the try block without running a finally block. In this example, we are specifying that our catch blocks handles exceptions of the type PathTooLongException and ArgumentException. We are also adding an if block that will throw an ArgumentException if there is a "\" character in the directory name.

try
{
Directory.CreateDirectory(MapPath(".") + "\\" + txtDir.Text);
// adding this line throws an ArgumentException even if the \ character is normally valid
if (txtDir.Text.Contains("\\"))
{
throw new ArgumentException("Directory name cannot contain the \"\\\" character");
}
}
catch (PathTooLongException ex)
{
lblStatus.Text = "There was an PathTooLongException when creating the directory";
lblStatus.Text += " in " + MapPath(".") + "\\" + "\r";
}
catch (ArgumentException ex)
{
lblStatus.Text = "There was a ArgumentException when creating the directory";
lblStatus.Text += " in " + MapPath(".") + "\\" + "\r";
lblStatus.Text += ex.Message + "\r";
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

We have one textbox,a Submit button, and a label on the front end for user interaction. The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Catching an Exception:</td>
<td align="center" bgcolor="#FFFFFF">
Directory:<asp:TextBox ID="txtDir" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /><br />
&nbsp;<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

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

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
Directory.CreateDirectory(MapPath(".") + "\\" + txtDir.Text);
// adding this line throws an ArgumentException even if the \ character is normally valid
if (txtDir.Text.Contains("\\"))
{
throw new ArgumentException("Directory name cannot contain the \"\\\" character");
}
}
catch (PathTooLongException ex)
{
lblStatus.Text = "There was an PathTooLongException when creating the directory";
lblStatus.Text += " in " + MapPath(".") + "\\" + "\r";
}
catch (ArgumentException ex)
{
lblStatus.Text = "There was a ArgumentException when creating the directory";
lblStatus.Text += " in " + MapPath(".") + "\\" + "\r";
lblStatus.Text += ex.Message + "\r";
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment