How to see exception detail in debugger without assigning variable to exception?
C#Visual StudioExceptionC# 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:
which gives you everything:
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.