2013-04-11 20 views
23

我想在Visual Studio調試器中查看異常細節,而無需將變量分配給異常。目前,我有寫是這樣的:如何在調試器中查看異常細節而不將變量分配給異常?

try 
{ 
    //some code 
} 
catch (SecurityException ex) 
{ 
    //some code or ever no any code 
} 

Visual Studio中拋出,表明前變量從未使用過的錯誤,但我需要這個變量,看異常詳細信息,同時調試。

更新:我知道如何抑制VS錯誤「變量永遠不會被使用」,問題是在沒有這個變量的情況下看到手錶內的異常。 @VladimirFrolov的$ exception變量或@MarcGravell的異常幫助程序是一個答案。

+0

添加罰球前;在你的捕獲?或者Console.WriteLine(ex.getMessage());或任何其他值 – Rob 2013-04-11 07:52:42

回答

50

你可以看到你的例外當地人列表或觀看列表中使用$exception

try 
{ 
    // some code 
} 
catch (SecurityException) 
{ // place breakpoint at this line 
} 
+0

VS.NET 2017仍然如此?似乎沒有vs2017 – 2017-11-20 09:46:02

+0

@FrederikGheysels,我總是在VS2017中使用'$ exception',它工作得很好。 – Andrew 2017-12-18 19:58:39

1

只寫

catch 
{//set breakpoint here 
} 
2

您可以從Visual Studio的使用功能。

調試=>異常=>檢查 「通用語言運行時例外」

就是這樣。希望能幫助到你。

2

就避開了警告:「變量‘EX’的聲明,但從未使用過」在catch語句,請執行下列操作:

try 
{ 
} 
catch (Exception) 
{ 
    // set break point 
} 

或者使用System.Diagnostics.Debug.WriteLine()或啓用跟蹤或調試才能使用trace listener

+2

你知道這不是Danila所問的,對吧? – Andrew 2017-03-29 01:55:54

+0

這甚至不試圖回答所問的問題。 – gdoron 2018-01-29 09:32:28

2

使用

catch (SecurityException /*without variable*/) 
{/*break Point*/ 
    //some code or ever no any code 
} 

catch /*without parameter*/ 
{/*break Point*/ 
    //some code or ever no any code 
} 

,但我認爲這是你的意思

catch (SecurityException ex) 
    { 
     MessageBox.Show(ex.ToString()); //for Winforms 
     Console.WriteLine(ex); //for console 
    } 
8

你不需要做任何事情:只要把一個斷點內catch(或在catch並且一次進入該塊),您應該看到邀請以查看異常助手。這適用於裸catch或特定類型catch(SecurityException)塊:

enter image description here

它給你的一切:

enter image description here

+0

我對這個對話框的問題是,如果「數據」集合中有消息,我似乎無法通過此對話框查看它們。有沒有辦法? – 2014-12-16 00:46:19

+5

Visual Studio 2015可能不再具有此功能嗎?至少它只是在沒有提供任何幫助的情況下進入'catch'塊,就像其他任何代碼行一樣,無論我在哪裏放置斷點。 – Andrew 2017-03-30 02:28:51

+1

@Andrew這也是我所見過的。我在這裏詢問了這個問題:https://stackoverflow.com/questions/47872167/where-did-the-exception-helper-go-for-visual-studio-2015 – 2017-12-18 16:12:43