Page 122 - DCAP312_WEB_TECHNOLOGIES_II
P. 122

Web Technologies-II



                   Notes         Typically, the first action you would achieve in this handler would be to obtain the exemption
                                 thrown, by using theServer.GetLastError method. This method would return a reference to the
                                 last Exception object that was thrown.
                                 After you get the exemption object, you will want to readdress the user to a mistake page. We
                                 could make ASP.NET do the redirection by using the error Page attribute of the Page (design
                                 time) or by using the Page.ErrorPageproperty (runtime). Obviously, the choice here would be
                                 to programmatically set the value using thePage.ErrorPage property in the event handler.
                                 If you do not specify an error page, the exception gets wrapped inside an HttpUnhandledException
                                 object and propagates. If you do not want the exception to be wrapped, then simply throw the
                                 last exception, which would force immediate propagation escaping any intervention. However,
                                 this would prevent ASP.NET from redirecting the user to a page specific page either. In other
                                 words, if you are going to throw the last error (or any exception for that matter), setting the
                                 error page will have no effect.
                                           private void WebForm1_Error(object sender, EventArgs e)
                                           {
                                           // Get the last exception thrown
                                           Exception ex = Server.GetLastError();


                                           // Do something with the exception like logging etc.
                                           // Set the error page
                                           this.ErrorPage = “/ErrorHandling/ErrorPages/BaseError.html”;
                                           }
                                 To reduce redundant code, you could define a base web form page which defines the Page.Error
                                 event handler and then wire up code in the constructor, and then make all your Web Form
                                 pages derive from this base page. This would save you the effort of writing the error handler
                                 in each web form.
                                 6.3.3 Application Level

                                 Attach an event handler to the Application.Error event.
                                 When an unhandled exception leaves a page, it gets propagated to the application level, which
                                 would trigger this event.
                                 There are two things you would want to do in an application error handler.
                                    •  Get the last exception thrown using Server.GetLastError.

                                    •  Clear the error using Server.ClearError, to inform ASP.NET that you have handled the
                                      error.

                                 However, since there is not any higher scope where the exception could be caught, ASP.NET is
                                 forced to handle it. The way ASP.NET handles the exception depends upon the settings specified
                                 in the customErrors section we saw before. If no settings are defined, ASP.NET would use the
                                 defaults and display the infamous ‘yellow’ error page.

                                                If you do not clear the error, the exception would propagate. It becomes
                                                difficult to handle the exception.

                                 6.3.4 HTTP Module Level
                                 As an alternative of handling application errors in worldwide.asax, exceptions may also be
                                 handled by attaching an HTTP Module which would have a handler attached to the submission


        116                               LOVELY PROFESSIONAL UNIVERSITY
   117   118   119   120   121   122   123   124   125   126   127