This article is a mirror article of machine translation, please click here to jump to the original article.

View: 45249|Reply: 1

[Source] .NET/C# threads catch exception information

[Copy link]
Posted on 9/23/2020 7:59:52 PM | | | |
Recently, I had an interview with a question about how to get thread exceptions, and I answered incorrectly. I also get exceptions by writing try catch outside the thread I said.

In the past, when I was doing winform software development, I often used threads, and there were two ways for me to obtain exception information:

  • Write try catch in the thread, and call the delegate to output the exception information to the software log text box.
  • Write a try catch in the thread, call the log framework, and write to the file.


I also didn't write try catch outside the thread, so why did I answer incorrectly?

Test code:



No exception information was obtained outside the thread, and the main thread died, no "OK" printed.

What should I do if I want to get the exception information of the thread in the main thread? You can use events, which are actually delegates.

Tutorial on the event:


The code is as follows:



(End)




Previous:asp.net Core source code address
Next:JS adapts to the screen size carousel graph
 Landlord| Posted on 4/14/2021 2:55:15 PM |
Exceptions using Task are not handled and do not affect the main thread



If the Thread exception is not handled, the main thread crashes



Test code:



Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com