Abnormal processing steps in ASP.NET

zhaozj2021-02-16  102

During the ASP.NET development process, we will encounter a lot of Exception, and if you do not deal with these Exception, you will have a very ugly page. There are also some unpredictable Exception, and we must also record the specific location when you happen, so that we cannot correct the error. The location of ASP.NET is about three places 1. In the code segment of the program, this is the most direct processing of exceptions. The following Try {n = convert.TOINT32 (INFO);} Catch (Exception) {} is just the most basic processing of exceptions.

2. APPLICATION_ERROR event in the ASP.NET .Application_ERROR event. This event will be triggered for any unprocessed exception thrown in the application. Generally, we deal with protected void application_ERROR (Object Sender, Eventargs E) {exception exp = server.getlasterror (); //rrorlog.log (EXP); / / =============== ===================== String stre = "internal error:" E.Nnerexception.tostring () "/ r / n stack:" E.STACKTRACE "/ r" "Message:" E.Message "/ R Source:" E.Source; log (stre); server.clearerror (); server.transfer ("error.aspx", false);} We can handle errors that the Server ends. We record the source of the error.

3. You can also handle code errors on page or application level. The base class discloses a page_error method, which can be rewritten on the page. This method is called whenever running an unusually caught anomaly. Void Page_ERROR (Object Source, Eventargs E) {String Message = "" "

" Request.url.toString () "" "
  "  server.getlasterror (). toString ()  "  " "  ";

Response.write (Message);

Below I told how to uniform processing exceptions in the ASP.NET program, we take the most common session expiration as an example.

Let's write a session expiration exception PUBLIC CLASS YSESSIONEXCEPTION: Exception {}

We redefine an attribute public int servicevalue {get {if (session ["sessionValue"] == null) {throw new ysessionException (");}}} Under Page_Error or Application_ERROR Processes this exception {Exception Exp = Server .Getlasterror (); if (exp is ysessionException) {................} Server.clearerror (); Server.Transfer ("Error.aspx", False; }

This allows us to provide a good Exception interface.

转载请注明原文地址:https://www.9cbs.com/read-12113.html

New Post(0)