In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you to use ASP.NET 2.0 error example analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.
We will use a simple process to record errors and exceptions in our website. We will do this: whenever we encounter a program error, navigate the user to a separate page. At the same time, the error will be logged to a text file on the server. Whenever an error occurs, we will record it every day in the form of a log. Having said so much, let's take a look at some code.
Step 1: first create an error folder to store the error log files. Right mouse button site > create a new folder. Name the folder Error. If there is no Web.config file in the site, please add one. Right-click site > add new project > Web.config.
Step 2: now we are going to create an error handling code. We just need to right-click site > add New Project > Select Class. Rename the class to ErrHandler.cs, and then click the add button. When you do this, a dialog box pops up as to whether you want to save this class file in "App_Code" and we choose to accept it.
Step 3: now let's add some functions to ErrHandler.class. This class is used to accept error messages and save them in a text file. Create such a text file every day. If the same file name already exists, the error message will be appended to the file. Otherwise, a new file is created and the error message is written to it.
The code looks like this:
/ / Handles error by accepting the error message / Displays the page on which the error occured public static void WriteError (string errorMessage) {try {string path = "~ / Error/" + DateTime.Today.ToString ("dd-mm-yy") + ".txt" If (! File.Exists (System.Web.HttpContext.Current.Server.MapPath (path) {File.Create (System.Web.HttpContext.Current.Server.MapPath (path)) .Close () } using (StreamWriter w = File.AppendText (System.Web.HttpContext.Current.Server.MapPath (path) {w.WriteLine ("\ r\ nLog Entry:"); w.WriteLine ("{0}", DateTime.Now.ToString (CultureInfo.InvariantCulture)) String err = "Error in:" + System.Web.HttpContext.Current.Request.Url.ToString () + ". Error Message: "+ errorMessage; w.WriteLine (err); w.WriteLine (" _ "); w.Flush (); w.Close ();}} catch (Exception ex) {WriteError (ex.Message) }}
This is our ErrHandler class. Then let's take a look at how to use this class and handle errors at the Page level (Application level).
Handling errors at the Page level
In Default.aspx, add a button control from the toolbox. Name the button btnError and set the value to "Throw Handled Exception". We will throw an exception. As long as we define a catch block, when an error occurs, it will be captured and registered in the Error folder. The text file will take the date of the day as the file name, and when there is no file, a new file will be created by the following code.
The button click operation code is as follows:
Protected void btnHandled_Click (object sender, EventArgs e) {try {throw new Exception ("Sample Exception");} catch (Exception ex) {/ / Log the error to a text file in the Error folder ErrHandler.WriteError (ex.Message);}}
Now, run the program and click the button. Because we have handled errors and recorded exceptions in the code, you will find that nothing seems to happen when the button is clicked. Close the program, refresh the Error folder, and you will see a new file with the name of today's date created. The exception has been successfully recorded as shown below. The date and time will be different on your machine.
Log Entry: 01Universe 11 23:33:46Error in: http://localhost:51087/ErrorHandling/Default.aspx. Error Message:Sample Exception__
Redirecting users on unhandled errors (redirects users without handling errors)
Let's look at how to catch errors that occur without error handling at the Application level and direct users to a different page.
To catch an error without error handling, simply do the following. Add a Global.asax file (right-click project > Add New Item > Glabal.asax). In the Application_Error () method, add the following code:
Void Application_Error (object sender, EventArgs e) {/ / Code that runs when an unhandled error occurs Exception objErr = Server.GetLastError (). GetBaseException (); string err = "Error in:" + Request.Url.ToString () + ".Error Message:" + objErr.Message.ToString (); / / Log the error ErrHandler.WriteError (err);}
We noticed that errors were caught by using the Server.GetLastError () function. When an error occurs without error handling, to redirect the user to a different page, what we need to do is to open your Web.config file, navigate to the tab and log it out. After removing the comment, the tag should look like this:
section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of an error stack trace. -- > < customErrorsmode= "RemoteOnly" defaultRedirect= "GenericErrorPage.htm" > < errorstatusCode= "403" redirect= "NoAccess.htm" / > < errorstatusCode= "404" redirect= "FileNotFound.htm" / > < / customErrors >
Will:
Mode= "RemoteOnly" tomode= "On" defaultRedirect= "GenericErrorPage.htm" to defaultRedirect= "ErrorPage.aspx"
Modified to:
< customErrorsmode= "On" defaultRedirect= "ErrorPage.aspx" > < errorstatusCode= "403" redirect= "NoAccess.htm" / > < errorstatusCode= "404" redirect= "FileNotFound.htm" / > < / customErrors >
This configuration file will direct the user to a page named ErrorPage.aspx. Let's create this error page and display some information to the user.
Right-click the website > Add New Item > create an ErrorPage.aspx, and then display a message on the page, informing the user that an error has occurred.
To test this functionality, we go back to Default.aspx, add a new button and name it btnUnhandled, and set the text property to Throw Unhandled Exception. Exe. We will use the "Divide By Zero" exception. Don't deal with it. We can find that the catch block is missing. So when an error occurs, the user will redirect to "ErrorPage.aspx" as we set in the web.confg file.
Protected void btnHandled_Click (object sender, EventArgs e) {int I = 9; int j = 0; Respone.Write (I / j);}
Run this program and click the "Throw Unhandled Exception" button. You will find that the user is automatically directed to the Error page. And the error is also recorded in the Error folder.
This is the end of the example analysis of errors in using ASP.NET 2.0. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.