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.