How to see exception detail in debugger without assigning variable to exception?

C#Visual StudioException

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

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

Solution 2 - C#

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

Solution 3 - C#

At any moment when you hit the exception you can check the Watch Window and add variable: $exception. This will let you work with all the Exception metadata.

Solution 4 - C#

You can use a functionality from Visual Studio.

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

That's it. Hope it helps.

Solution 5 - C#

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
    }

Solution 6 - C#

just write

 catch
{//set breakpoint here
}

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanila PolevshchikovView Question on Stackoverflow
Solution 1 - C#VladimirView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow
Solution 3 - C#Carlos ToledoView Answer on Stackoverflow
Solution 4 - C#AndyView Answer on Stackoverflow
Solution 5 - C#PyromancerView Answer on Stackoverflow
Solution 6 - C#user1064248View Answer on Stackoverflow
Solution 7 - C#KF2View Answer on Stackoverflow