Consider this class:
class Person : IDisposable
{
public Person(int age)
{
}
public void Dispose()
{
Console.WriteLine("Dispose");
}
}
and this code:
using (Person perosn = new Person(0))
{
using (Person child = new Person(1))
{
}
}
When run this code output is two Dispose
in console.
I change the class:
class Person : IDisposable
{
public Person(int age)
{
if (age == 1)
{
throw new Exception("Person");
}
}
public void Dispose()
{
Console.WriteLine("Dispose");
}
}
I get exception and both class does not dispose.
So why when I throw new exception in child's constructor, both classes are not disposed?