[英]How can I change “Break When an Exception is Thrown” debugger behaviour from code
[英]Debugger does not break the code when exceptions are thrown
調試Winform
程序時,我最近發現,該程序不會彈出破壞性能的源代碼行,而是會彈出一個對話框,顯示錯誤消息,如下所示:
這對我不利,因為我不知道導致此失敗的代碼在哪里,您知道為什么我的Visual Studio
調試器會表現為這種行為,以及如何更改此行為?
如果您在Non-Debug
模式下運行應用程序,則不會破壞您的代碼,它只會在MessageBox
顯示錯誤消息
如果要拋出異常並精確指向引發異常的代碼,則需要在“ Debug
模式下運行程序。
編輯:如果您已經處於調試模式,請嘗試以下操作:
步驟1:在VS IDE中轉到“ Debug
菜單
步驟2:選擇Exceptions
步驟3:現在,您需要在“ Exceptions
對話框中選中“ Common Language Runtime Exceptions
選項。
我想您會在程序中捕獲異常,並在這種情況下顯示一個消息框。 可能是一針見血catch (Exception e)
。 您可以使調試器闖入任何引發的異常,即使在Debug> Exceptions下也是如此。
您可以顯示StackTrace
而不是Message
,其中包含對導致異常的調用的深入分析。
您可以這樣顯示您的消息:
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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.