Consider the following code: <%@ Page Language="C#" %> <html> <head> <script runat="server"> private void btnCalculateClick(object sender, EventArgs e) { double side; double perimeter, area; side = double.Parse(txtSide.Text); perimeter = side * 4; area = side * side; txtPerimeter.Text = perimeter.ToString(); txtArea.Text = area.ToString(); } </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> </form> </body> </html> Here is an example of using the web page: Normally, the user should enter a number in the Side text box and click Calculate. But so more reason, a user may enter a bad value. Here is an example: Then: To handle an exception in your code, the formula to follow is: try { // Try the program flow } catch { // Catch the exception } You start with a section that uses the try keyword. This section has an opening and a closing curly brackets that constitute its body. In the body of the try block, write the normal code this section is supposed to deal with. 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(); } } </script> Under the try block, create a new block that uses the catch keyword and has a body delimited by an opening and a curly brackets. You must always have a catch section. That is, a try block cannot be used alone. 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(); } catch { } } </script> Here is how this works. In the try block, the normal code is run. If there is a problem, the execution stops in the try block and gets out of it. Then it starts looking for a catch section, which we created. The catch section takes over.
If an error occurs when the code in the try section executes, the execution is transferred to the next catch section. You can then use the catch section to deal with the error. At a minimum, you can display a message to inform the user. 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 { lblMessage.Text = "There was a problem with the program"; } } </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 page: Then:
|
|||||||||
|