When an exception is thrown or encountered:
void ThrowException()
{
try
{
throw new Exception("Error");
}
catch
{
}
}
is it & how is it disposed from memory?
and how does the above code differ from the below code in respect of the disposal from memory of the Exception object?
void ThrowException()
{
try
{
throw new Exception("Error");
}
catch(Exception e)
{
}
}
Exception does not inherit from IDisposable so it does not need to be disposed. Memory deallocation is done by the GC like for all .NET objects.
The Exception instance simply acts as another object in memory - after the catch method, there aren't any remaining references to the instance, so it will get removed in the next garbage collection sweep.
Exceptions generally don't inherit from IDisposable, as there shouldn't really be external resources associated with an Exception. If you do. have an IDisposable exception, you should look really hard at whether your architecture & code design is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With