How to see exception detail in debugger without as

2019-01-23 22:48发布

I want to see exception detail in visual studio debugger without assigning variable to exception. Currently I have to write something like this:

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

Visual studio throws an error indicating that ex variable is never used, but i need this variable to see exception detail while debugging.

UPDATE: I know how to suppress VS error 'variable is never used', problem is in seeing exception inside watch without this variable. $exception variable by @VladimirFrolov or exception helper by @MarcGravell is an answer.

6条回答
Explosion°爆炸
2楼-- · 2019-01-23 23:15

just write

 catch
{//set breakpoint here
}
查看更多
▲ chillily
3楼-- · 2019-01-23 23:17

You can see your exception in Locals list or use $exception in Watch list:

try
{
    // some code
}
catch (SecurityException)
{ // place breakpoint at this line
}
查看更多
我命由我不由天
4楼-- · 2019-01-23 23:20

use

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

or

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

but i think this is what you mean

catch (SecurityException ex)
    {
       MessageBox.Show(ex.ToString()); //for Winforms
       Console.WriteLine(ex); //for console
    }
查看更多
贼婆χ
5楼-- · 2019-01-23 23:25

You can use a functionality from Visual Studio.

Debug => Exceptions => Check "Common Language Runtime Exceptions"

That's it. Hope it helps.

查看更多
小情绪 Triste *
6楼-- · 2019-01-23 23:35

You don't need to do anything: just put a breakpoint inside the catch (or on a catch and step once into the block) and you should see an invitation to see the exception helper. This works for naked catch or for type-specific catch(SecurityException) blocks:

enter image description here

which gives you everything:

enter image description here

查看更多
放荡不羁爱自由
7楼-- · 2019-01-23 23:38

To avoid getting the warning: "The variable 'ex' is declared but never used" in a catch statement,do the following:

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

Or use System.Diagnostics.Debug.WriteLine() or Enable tracing or debugging to use a trace listener.

查看更多
登录 后发表回答