I am writing a C# application and have stumbled across something that makes me unsure if it is a Visual Studio bug, or my knowledge is incorrect.
The culprit is a using statement inside "catch" part of a try/catch, where Visual Studio 2019 shows a warning that a disposable object will not be disposed:
Disposable object created by 'MakeMeSomeDisposableObject()'s is never disposed
The same statement used outside the catch, shows no warning message.
Here is an example of what I am talking about:
try
{
SomeMethodThrowingExceptions();
}
catch
{
using(var someDisposableObject = MakeMeSomeDisposableObjects())
{
someDisposableObject.SaveTheWorldFromException();
}
}
My understanding is that in case of an exception - the disposable object will save the world and get disposed of after finishing its job because of the using statement. Visual Studio however thinks otherwise and shows a warning message, but also shows no fix - is this a VS bug or am I wrong about how using statements work in a catch?