In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the common ways to catch exceptions in asp.net development". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common ways to catch exceptions in asp.net development?"
1. BasePage class processing method
Override the OnError event in the common base class of the page. In the previous article, "asp.net implements a very useful custom page base class", Lou Pig has posted the code, so it is no longer troublesome. According to experience, many people almost always write this when developing, and it is very helpful for debugging and maintenance. It is important to note that for each new page added, its corresponding class must inherit from the BasePage class exception handling to work.
2. Global.asax processing method
As mentioned in 1, the exception handling of the BasePage class requires that every aspx class file inherit it, and its applicability and performance are obviously compromised. The Global.asax file defines the methods, properties, and events common to all application objects in asp.net applications. We can implement Application_Error events and handle them in Global.asax instead of using the BasePage approach. The following mimics the exception handling method in the BasePage class, which is implemented as follows:
/ error handling: log, navigate to the public error page / protected void Application_Error (object sender, EventArgs e) {if (Server.GetLastError () = = null) return; Exception ex = Server.GetLastError () .GetBaseException (); string error = this.DealException (ex); DotNet.Common.Util.Logger.WriteFileLog (error, HttpContext.Current.Request.PhysicalApplicationPath + "LogFile") If (ex.InnerException! = null) {error = this.DealException (ex); DotNet.Common.Util.Logger.WriteFileLog (error, HttpContext.Current.Request.PhysicalApplicationPath + "LogFile");} this.Server.ClearError (); this.Response.Redirect ("/ Error.aspx") } / handle the exception to write the main exception information to the text log / private string DealException (Exception ex) {this.Application ["StackTrace"] = ex.StackTrace; this.Application ["MessageError"] = ex.Message; this.Application ["SourceError"] = ex.Source; this.Application ["TargetSite"] = ex.TargetSite.ToString () String error = string.Format ("URl: {0}\ nmethod of throwing exception: {1}\ nError message: {2}\ nError Stack: {3}\ n", this.Request.RawUrl, ex.TargetSite, ex.Message, ex.StackTrace); return error;}
The advantage of the above approach is that if you write the code once, it will catch and handle most of the exceptions that occur in the application. Lou Pig would like to express a heartfelt emotion here, thanks to ms for providing us with such an excellent framework, it is too easy.
3. IHttpModule interface processing
The handling methods of 1 and 2 are very familiar to everyone. Lou Pig basically follows the above two writing methods in the actual development, and Lou Pig has even excitedly thanked ms because of the size-all treatment in 2. However, when the asp.net program calls the thread for asynchronous processing, the exceptions that are easily thrown in the background thread or thread pool can not be fully caught by 1 or / and 2, which involves the handling of uncaught exceptions under asp.net. In other words, the handling of exceptions in many large and small projects that Lou Pig has done before is incomplete. Is this the bad result that the nc building pig did not thank the country in advance? Thank the country, thank ms, thank the blog park, thank the selfless xdjm, thank yourself.
The handling steps for uncaught exceptions under asp.net are as follows:
(1) create a class that implements the IHttpModule interface
Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text;namespace DotNet.Common.WebForm {using DotNet.Common.Util; / generic uncaught exception handling / public class AspNetUnhandledExceptionModule: IHttpModule {static object syncObj = new object (); static bool isInit = false Public AspNetUnhandledExceptionModule () {} # region IHttpModule Methods public void Init (HttpApplication context) {lock (syncObj) {if (! isInit) {AppDomain.CurrentDomain.UnhandledException + = new UnhandledExceptionEventHandler (OnUnhandledException); isInit = true } public void Dispose () {} # endregion # region OnUnhandledException void OnUnhandledException (object o, UnhandledExceptionEventArgs e) {if (e.ExceptionObject = = null) return; Exception ex = e.ExceptionObject as Exception String error = string.Format ("method to throw exception: {0}\ nError message: {1}\ n error Stack: {2}\ n", ex.TargetSite, ex.Message, ex.StackTrace); Logger.WriteFileLog (error, AppDomain.CurrentDomain.BaseDirectory + "LogFile");} # endregion}}
(2) web.config node configuration
Finally, post the test code:
Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {System.Threading.ThreadPool.QueueUserWorkItem (new System.Threading.WaitCallback (Test), null);} protected void Test (object state) {int [] numArr = new int [100]; numArr [100] = 100; / / exception}
It is important to note that for programs handled through threads or thread pools, each thread will have its own independent context when an exception occurs, so HttpContext objects should appear as little as possible in the exception handling phase.
Thank you for your reading. The above is the content of "what are the common ways of catching exceptions in asp.net development". After the study of this article, I believe you have a deeper understanding of the common ways of catching exceptions in asp.net development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.