Vous êtes sur la page 1sur 3

ACS4134 Internet Programming

Tutorial 9 (Solution)

1. There are four different types of errors that might occur during the development of a
Web application, which are. parser error compilation error configuration error runtime error Describe and differentiate the four types of errors above. Provide example to support your answers. Please refer to notes

2. Exception handling is a mechanism to allow us to catch an exception gracefully.


a. Explain the meaning of exception. The exceptions are any error condition or unexpected behavior that occurs during the execution of a program, and consequently disrupts the normal flow of execution. They can be because of user, logic or system errors. b. Explain the importance of exception handling. To detect and to handle run time errors.

c. There are 3 different exception handling mechanisms that we can use to handle
an exception. State the 3 mechanisms and differentiate them. Please refer to notes

3. Refer to the following code in Figure 1, answer the following questions.


Line Line Line Line Line Line Line 1: 2: 3: 4: 5: 6: 7: Void btnCal_Click(object sender, EventArgs e) { double Total = Convert.ToInt32(txtTotal.Text); double Amount = Convert.ToInt32(txtAmount.Text); lblAnswer.Text = Convert.ToString(Amount / Total); lblMsg.Text = ; } Figure 1

a. Assume there is no validation of the users inputs is done before the


textboxes values are passed to the server, therefore there are two potential errors that may occur when the button btnCal is clicked. Identify the type of the potential errors and explain what are the two potential errors. Run-time error. 1) If the user did not enter any value or non-numeric data, the data cannot be converted correctly.

Chapter 9 Error Handling and Debugging

ACS4134 Internet Programming

2) If the user entered 0 in txtTotal, then there will be a DivideByZeroException in


Line 5.

b. We

can use try...catch, Page_Error() method or Application_Error() method to catch the potential errors. Suggest which approach is better to catch the potential errors above, and justify your answer.

Both answers can be accepted (but try...catch maybe better). Student must explain his/her choice accordingly. Examples of answers: You are just testing a block of code that could fail, and you are anticipating a particular exception that may occur. Therefore it is easier to use TryCatch block. If you use a trycatch block, a problem is more easily corrected where it occurs. If the user can help correct a problem, the page needs to return to the same place so the user has a context for understanding what to do. A page-level handler returns you to the page, but there is no longer anything on the page because instances of controls are not created. For Page_Error event handler, to provide the user any information, you must specifically write it to the page. Unlike Page_Error(), trycatch can handle specific exception, such as DivideByZeroException, and return its message easily. You would probably use a page-level error handler to log unhandled errors or to take the user to a page that can display helpful information. So if the exception is already anticipated, it is better to use TryCatch statement. [Reference: http://msdn.microsoft.com] c. Write the necessary C# code to demonstrate each of the approaches below to handle the potential errors in Figure 1.

i. try...catch ii. Page_Error() method


You are required to display the exception message on the Label lblMsg. try { double Total = Convert.ToInt32(txtTotal.Text); double Amount = Convert.ToInt32(txtAmount.Text); dlblAnswer.Text = Convert.ToString(Amount / Total); } catch (Exception ex) { lblMsg.Text = ex.Message.ToString(); } protected void Page_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError().GetBaseException(); Response.Write(ex.Message.ToString()) Server.ClearError(); } 4. Discuss the benefits of using custom error pages in an application.
Chapter 9 Error Handling and Debugging

ACS4134 Internet Programming

To avoid users to see the technical details of exception, which we can provide them a userfriendly error page

5. Demonstrate by writing the necessary code in the web.config file, so that if an


unauthorized user attempts to access to a restricted page, the NoAccess.htm will be displayed. In addition, if there is any run-time error occurred anywhere in the application, the apologize.htm will be displayed (instead of allowing the user to see the technical exception details). <customErrors mode=On defaultRedirect=apologize.htm> <error statusCode=401 redirect=NoAccess.htm /> </customErros> For more information, can refer to Hypertext Transfer (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)

Protocol -- HTTP/1.1

Chapter 9 Error Handling and Debugging

Vous aimerez peut-être aussi