C:\work\PassThruExc> tree /f
Folder PATH listing for volume OSDisk
Volume serial number is F6C4-7BEF
C:.
│ PassThruExc.csproj
│ PassThruExc.sln
│ Program.cs
│
└───Properties
launchSettings.json
< /code>
C:\work\PassThruExc> cat .\PassThruExc.csproj
Exe
net472
< /code>
C:\work\PassThruExc> cat .\Program.cs
using System;
using System.Collections.Generic;
internal static class Program
{
private static void Main(string[] args)
{
bool passThru = args.Length != 0 && Convert.ToBoolean(args[0]);
try
{
try
{
_ = new Dictionary()["hello"];
}
finally
{
Console.WriteLine("done.");
}
}
catch (Exception exc) when (!passThru)
{
Console.Error.WriteLine(exc);
}
}
}
C:\work\PassThruExc>
< /code>
I have Visual Studio 2022 setup as a debugger for unhandled exceptions.
I am going to run the program in 3 modes:
- No pass thru
- With pass thru - the debug dialog opens, but I cancel it
- With pass thru - the debug dialog opens and I accept to debug it. However, I will detach the debugger right away.
Please, observe:
Mode 1
C:\work\PassThruExc> .\bin\Debug\net472\PassThruExc.exe
done.
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary2.get_Item(TKey key)
at Program.Main(String[] args) in C:\work\PassThruExc\Program.cs:line 13
C:\work\PassThruExc>
< /code>
Mode 2
C:\work\PassThruExc> .\bin\Debug\net472\PassThruExc.exe true
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary2.get_Item(TKey key)
at Program.Main(String[] args) in C:\work\PassThruExc\Program.cs:line 13
done.
C:\work\PassThruExc>
< /code>
Mode 3
C:\work\PassThruExc> .\bin\Debug\net472\PassThruExc.exe true
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary2.get_Item(TKey key)
at Program.Main(String[] args) in C:\work\PassThruExc\Program.cs:line 13
C:\work\PassThruExc>
< /code>
My question is - why the finally block is bypassed when the debugger is actually attached?
P.S.
I use VS 17.14.2
Подробнее здесь: https://stackoverflow.com/questions/796 ... dled-excep