I attach a debugger by passing ‘-d’ as a command line parameter to my console app. That causes the following code to be called;
bool attachedDebugger = false;
try
{
attachedDebugger = System.Diagnostics.Debugger.Launch();
}
catch (Exception) { }
finally
{
Console.WriteLine(attachedDebugger ? "Debugger Attached" : "Failed to attach debugger");
}
After the Visual Studio 2010 JIT window pops up I sometimes change my mind and don’t want to debug, so I dismiss the dialog. If I don’t attach one then the Application exits immediately without anything being written to the console.
I know that this it a bit of an edge use case, I should just remove the ‘-d’ from the command line if I don’t want to debug. The reason for my question is that I wish to understand what’s happening.
I thought the finally block is always called, furthermore I would expect my application to continue if we fail to attach a debugger.
- Why is nothing printed to the console
if I decline attaching a debugger? - Does
Debugger.Launch()call
System.Exiton failure to attach?
EDIT Thanks @Moo-Juice I now know that a return value of false implies a debugger was already attached but the questions above remain unresolved.
This is a reported bug in dotnet 4.0