Troubleshooting for unhandled WinForm exceptions in asp.net

Source: Internet
Author: User

I don't know how many exceptions can be handled in the inForm program. I have encountered one today, So that I cannot obtain the exception information, which is very bad for the user experience, later I found out why this happened in my code, because I used to place the entire statement block package in the outermost layer to obtain possible exceptions in the middle.

But this Code uses the BeginInvoke statement block, and I use the anonymous method, so exceptions in the anonymous method cannot be caught outside. Therefore, exceptions that cannot be processed may occur. The correct code should be as follows:

The Code is as follows: Copy code

This. BeginInvoke (new MethodInvoker (delegate ()
{
Bool flag = false;
Try
{
Flag = service. Authentication (); // exceptions may occur here
If (flag)
{
This. DialogResult = DialogResult. OK;
}
Else
{
MessageBox. Show ("the user name or password is incorrect. "," Warning ", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}
Catch (Exception ex)
{
Program. Equality. ShowBalloonTip (2000, "application Error", ex. Message, ToolTipIcon. Error );
}
}));

WebService is called in the code above, but the connection will fail when the network is disconnected, because it is written in the anonymous method, so this. the tryf block outside BeginInvoke cannot capture the exception information generated by WebService. Therefore, you must write the tryf block in it.

Related Article

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.