Dot-Net

Visual Studio 2012 RC 中斷 .NET Framework 內的異常。如何使其僅在我的程式碼中中斷異常?

  • February 11, 2014

使用 Visual Studio 2012 RC 調試並打開異常中斷時,Visual Studio 會中斷 .NET Framework 內的異常。如何使其僅在我的程式碼中中斷異常?

在調試 ASP.NET MVC 4 項目時,每次點擊頁面都會引發很多框架異常。以下異常經常發生:

System.Globalization.CultureNotFoundException occurred
 HResult=-2147024809
 Message=Culture is not supported.
Parameter name: name
UserCache is an invalid culture identifier.
 Source=mscorlib
 ParamName=name
 InvalidCultureName=UserCache
 StackTrace:
      at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
 InnerException: 

此異常來自 mscorlib.dll,我不希望 Visual Studio 中斷。

看起來我在例外視窗中缺少一列。我只看到“拋出”列並且缺少“使用者未處理”。這可能是我的問題。

我相信你需要做的就是去

調試 -> 選項 -> 調試並選中Enable Just My Code (Managed Only)

轉到Debug -> Exceptions並取消勾選所有內容。這將告訴調試器僅在未處理的異常上中斷。未處理的異常是未“擷取”的異常,它們不會在適當的 try/catch 中發生。

但是要明白,無論發生哪種類型的異常,如果不處理,調試器將中斷或應用程序將退出。這就是它應該如何工作的方式。如果您希望發生異常,請將罪魁禍首程式碼包裝在適當的 try/catch 塊中。

在這種情況下,那將是

try
{
...
}
catch (CultureNotFoundException ex)
{
  // Show your own message here, or otherwise handle the exception
}

引用自:https://stackoverflow.com/questions/11741968