How to solve the problem that the winform program does not capture exceptions

Source: Internet
Author: User

In developmentWinformProgramMulti-thread is used, and the program is always automatically shut down after the server is deployed. The symptoms are described as follows:

InWindows Server 2003On the operating system, run. NET 2.0Or3.5In the event viewer.Eventtype clr20r3 P1.... p2... PNError message.

The possible cause is that an exception occurs in the application. NetNo or cannot catch this exception, so the following situation occurs:

1.The program is automatically closed.

2.You can seeDw20.exeWill automatically disappear soon

3.SimilarEventtype clr20r3 P1.... p2... PNError message.

 

 

Solution 1:.InConfigFile<Configuration> <runtime> <legacyunhandledexceptionpolicy enabled = "true"/> </runtime> </configuration>In this way, the main program will not be forced to close when it encounters an uncaptured exception, but to ensure the stability and security of the program, you should find the place where the exception is thrown and modify it.

Solution 2:

used unhandledexceptioneventhandler event. With this event, we can intercept uncaptured exceptions, and process it.

event parameters for this event unhandledexceptioneventargs e , there are two attributes: exceptionobject . This attribute is returned as an object instance that intercepts exceptions. Another attribute is isterminating indicates whether the exception causes application termination. It must be noted that for . net1.1 and . net2.0 and above are different. . net1.1 the application is terminated only when exceptions are not caught in the main thread, . net2.0 and later versions always terminate the application.

In the main formMainLower, addAppdomain. Currentdomain. unhandledexception + =New Unhandledexceptioneventhandler(Unhandledexceptioneventhandler );

And codeUnhandledexceptioneventhandlerEvent,

Static VoidUnhandledexceptioneventhandler (ObjectSender,UnhandledexceptioneventargsE)

{

Try

{

Loghelper. Errorlog (E. exceptionobject. tostring (); // loghelperIs the log writing class. Here, you can directly write it to a file.

}

Catch

{

}

}

Run the program, capture exceptions, locate the problemCode.

This article ends.

Refer:

Http://hi.baidu.com/ymwang/blog/item/07b1b20eab9cdde537d12265.html(Important)

 

Http://www.cnblogs.com/xclw/archive/2009/09/29/1576436.html

 

Http://www.cnblogs.com/dudu/archive/2006/05/07/393446.html

 

Http://hi.baidu.com/xiejl/blog/item/98743c1f161cafc1a686697e.html

 

Http://topic.csdn.net/t/20050304/22/3825910.html

 

Http://www.yesky.com/258/1653758.shtml

Http://www.cnblogs.com/eaglet/archive/2009/02/17/1392191.html (Important)

 

Http://www.cnblogs.com/cgzwwy/archive/2008/08/31/1280597.html

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.