Use custom exception to restart your application (continued)

zhaozj2021-02-16  53

(Connecting) When the custom exception handling class is added, it is incorporated into the structure in the main function (in front of Application.Run), when the second line is attached to the TreadException event when the second row is attached to the TREADEXCEPTION event. The first line will instantiate the previously defined class, and OntReadexception will be automatically called after an exception is abandoned.

CustomExceptionHandler EH = New CustomExceptionHandler ();

2. Application.threadException = new system.threading.

3. ThreadExceptionEventHndler (EH.OnthreadException);

Now, start testing, add two buttons as shown in the program, just as you can see, I have tested that the exception that is not processed will cause the program to rush.

To the "Handled Exception" button, click the following code:

4. Private void btnhandledException_click (Object Sender,

5. System.Eventargs E)

6. {

7. TRY

8. {

9. Throw new Exception ("Handled Exception");

10.}

11. Catch (Exception EX)

12. {

13. MessageBox.show (ex.Message);

14.}

15.}

To the "Unhandled Exception" button, press the button to add the following code:

16. Private void btnunhandException_click (Object Sender,

17. System.EventArgs E)

18. {

19. Throw New Exception ("Unhandled Exception !!");

20.}

When running the application, when you press the "Handled Exception" button, only the window of the message text set in advance will be returned. When pressing the "Unhandled Exception" button, the display result is written to the log will revolution. Figure:

Click Here Download Source Code

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

New Post(0)