Наконец, блок не выполняется, когда VS прикреплен в результате невозможного исключения [дублировать]C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Наконец, блок не выполняется, когда VS прикреплен в результате невозможного исключения [дублировать]

Сообщение Anonymous »

у меня есть это небольшое приложение: < /p>
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.
The result of the run in mode 3 is somewhat surprising to me - the finally block does not run.
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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»