Although I am aware of the working of both finally and finalizer I have a clarification which needs to be cleared .
I have a situation where I am opening a connection to Datbabase instance and performing operations. Like
try
{
//Open a connection to db
//Perform some operations.
}
catch(Exception e)
{
}
finally
{
//Call the dispose method
}
Here I know that finally will be called all the time so the dispose will happen every single time.
Next ,in the finalizer I dispose the connection.
public class DbConnection
{
~DbConnection()
{
//Call the dispose method
}
}.
The same operation , is being performed by both the methods i.e., release unmanaged resources . Although there is problem sometimes with finalizer , like "Handler closed by finalizer before it is being accessed" , then why would I go with finalizer? In this scenario is it feasible to go with finally approach for my situation?