In my project I have an object whose constructor can throw. So the code I'm using all across is as follows:
MyObject obj = null;
try
{
obj = new MyObject();
obj.DoSomething();
}
finally
{
if (obj != null)
obj.Free();
}
As meantioned in Uses of "using" in C#, the code like
using (MyObject obj = new MyObject())
{
obj.DoSomething();
}
is converted by the .NET CLR to
{
MyObject obj = new MyObject();
try
{
obj.DoSomething();
}
finally
{
if (obj != null)
((IDisposable)obj).Dispose();
}
}
The question is: can I somehow make CLR put object's constructor into a try block?