Exceptions in the .NET Framework |
|
|
To support exception handling, the .NET Framework provides
a class named Exception. When an error is encountered, the
Exception class allows you to identify the type of error and take an appropriate action.
Exception mostly serves as the
general class of exceptions. Because there are various types of errors that
can occur on a web page, there are also many classes that are meant to
handle their corresponding types of errors.
|
Practical
Learning: Introducing .NET Exceptions
|
|
- Start Microsoft Visual
Studio or Microsoft Visual Web Developer
- To create a web site, on the
main menu, click File -> New -> Web Site... (or File -> New Web
Site)
-
Make sure the Language combo box is set to Visual C#.
Change the name of the web site to geometry5b
- Click OK
- In the Solution Explorer, right-click Default.aspx and click Rename
- Change the name to index.aspx and press Enter
- Change the file as follows:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="index.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style>
.maintitle
{
color: blue;
font-family: 'Times New Roman' , Garamond, Georgia, Serif;
text-align: center;
font-size: 24pt;
font-weight: bold;
text-decoration: none;
}
</style>
<title>Geometry: Square</title>
</head>
<body>
<table border="0" style="width: 500px">
<tr>
<td style="width: 100%">
<p class="maintitle">Geometry: Square</p>
</td>
</tr>
</table>
<form id="frmSquare" method="post" runat="server">
<div>
<hr />
<table>
<tr>
<td style="width: 100px">Side:</td>
<td style="width: 121px">
<asp:TextBox ID="txtSide"
runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:Button ID="btnCalculate"
runat="server"
Text="Calculate" /></td>
</tr>
<tr>
<td style="width: 100px">Perimeter:</td>
<td style="width: 121px">
<asp:TextBox ID="txtPerimeter"
runat="server">
</asp:TextBox></td>
<td style="width: 100px"></td>
</tr>
<tr>
<td style="width: 100px">Area:</td>
<td style="width: 121px">
<asp:TextBox ID="txtArea"
runat="server">
</asp:TextBox></td>
<td style="width: 100px"></td>
</tr>
</table>
<table style="width:500px; text-align:center;">
<tr>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
- In the lower-left section of the window, click the Design button and, on
the form, double-click the Calculate button
- Implement the event as follows:
protected void btnCalculate_Click(object sender, EventArgs e)
{
double side = 0.00;
double perimeter, area;
try
{
side = double.Parse(txtSide.Text);
perimeter = side * 4;
area = side * side;
txtPerimeter.Text = perimeter.ToString();
txtArea.Text = area.ToString();
}
catch
{
}
}
|
- Save the file
Errors are dealt with in the catch
section. On the right side of
catch, open a parenthesis, declare a variable
of the type of exception you want to deal with. By default, an exception
is first of type Exception. Based on this, a typical formula to
implement exception handling is:
try
{
// Process the normal flow of the program here
}
catch(Exception e)
{
// Deal with the exception here
}
When an exception occurs in the try section,
code compilation is transferred to the catch section. If you
declare the exception as an Exception type, this class will
identify the error. One of the properties of the Exception class is
called Message. This property contains a string that describes the
type of error that occurred. You can then access this Exception.Message
property to display an
error message if you want. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>
<script runat="server">
private void btnCalculateClick(object sender, EventArgs e)
{
double side;
double perimeter, area;
try {
side = double.Parse(txtSide.Text);
perimeter = side * 4;
area = side * side;
txtPerimeter.Text = perimeter.ToString();
txtArea.Text = area.ToString();
// If no error occurred, don't show any message
lblMessage.Text = "";
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
}
</script>
<title>Exercise</title>
</head>
<body>
<form id="frmExercise" runat="server">
<table>
<tr>
<td></td>
<td><h3>Square Evaluation</h3></td>
<td></td>
</tr>
<tr>
<td>Side:</td>
<td><asp:TextBox id="txtSide" runat="server"></asp:TextBox></td>
<td><asp:Button id="btnCalculate"
Text="Calculate"
OnClick="btnCalculateClick"
runat="server"></asp:Button></td>
</tr>
<tr>
<td>Perimeter:</td>
<td><asp:TextBox id="txtPerimeter" runat="server"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td>Area:</td>
<td><asp:TextBox id="txtArea" runat="server"></asp:TextBox></td>
<td></td>
</tr>
</table>
<table>
<tr>
<td><asp:Label id="lblMessage" runat="server"></asp:Label></td>
</tr>
</table>
</form>
</body>
</html>
Here is an example of testing the web page:
As you can see, one of the strengths of the Exception.Message
property is that it gives you a good indication of the type of error that occurred. Sometimes, the message provided by the Exception class
is not clear enough. As an
alternative, you can create your own message and display it to the user.
Here is an example:
<script runat="server">
private void btnCalculateClick(object sender, EventArgs e)
{
double side;
double perimeter, area;
try {
side = double.Parse(txtSide.Text);
perimeter = side * 4;
area = side * side;
txtPerimeter.Text = perimeter.ToString();
txtArea.Text = area.ToString();
// If no error occurred, don't show any message
lblMessage.Text = "";
}
catch(Exception ex)
{
lblMessage.Text = "The calculation was not carried because " +
"the value you entered for the side is invalid";
}
}
</script>
Here is an example of testing the web page:
You can also combine the Exception.Message
message with your own message:
<script runat="server">
private void btnCalculateClick(object sender, EventArgs e)
{
double side;
double perimeter, area;
try {
side = double.Parse(txtSide.Text);
perimeter = side * 4;
area = side * side;
txtPerimeter.Text = perimeter.ToString();
txtArea.Text = area.ToString();
// If no error occurred, don't show any message
lblMessage.Text = "";
}
catch(Exception ex)
{
lblMessage.Text = "Error: " + ex.Message + " Contact the " +
"web master.";
}
}
</script>
Here is an example of testing the web page: