简体   繁体   中英

Debugger does not break the code when exceptions are thrown

When debugging my Winform program, I recently found that instead of breaking in the source code lines that do bad, the program will pop up a dialog showing error message, something like below:

在此处输入图片说明

This is not good for me as I didn't know where in the code that caused this failure, do you know why my Visual Studio debugger behaves like this and how can I alter this?

if you are running your application in Non-Debug mode it will not break your code ,it just displays the error message in MessageBox

if you want to throw exception and point to your code exactly where exception raised you need to Run you program in Debug mode.

EDIT: if you are already in Debug mode try this:

Step 1: Goto Debug menu in VS IDE
Step 2: Select Exceptions
Step 3: now You need to check the Common Language Runtime Exceptions option in Exceptions dialog.

在此处输入图片说明

I guess you catch exceptions in your program and show a message box in that case. Probably with a blanket catch (Exception e) . You can make the debugger to break into any exception thrown, even if caught under Debug > Exceptions.

You could show the StackTrace instead of the Message , which contains a drill down to the call that caused the exception.

You could show your message like this:

try
{
    // some code that throws an exception
}
catch()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("Message: {0}", e.Message);
    sb.AppendLine();
    sb.AppendLine();
    sb.AppendFormat("StackTrace: {0}", e.StackTrace);
    MessageBox.Show(sb.ToString(), "Error");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM