I open an excel file within my try
block.
Then in the catch
block, I put a statement to close that excel file as long as the excel application if any exception. I even tried finally
and it doesn't work neither.
It appeared that there is an error message stating the variable doesn't exist in the current context.
How should I do to close the excel when exception happens?
I open an excel file within my try block.
Open the excel file immediately before your try
block. This will allow it to be in scope during the catch
portion. If you plan on always closing it, just put the logic in the finally
block, as that will happen whether or not the exception occurs.
// Open file..
try
{
// use file
}
finally
{
// close here
}